Javascript 对象文字和方法分组
有人能告诉我如何用“对象文字”的方式写下面的内容吗Javascript 对象文字和方法分组,javascript,object-literal,Javascript,Object Literal,有人能告诉我如何用“对象文字”的方式写下面的内容吗 my.item('apple').suffix("is awesome") // console.log >> apple is awesome 我的尝试。。。但它显然不起作用 var my = { item:function(item){ my.item.suffix = function(suffix){ console.log(item, suffix); }
my.item('apple').suffix("is awesome")
// console.log >> apple is awesome
我的尝试。。。但它显然不起作用
var my = {
item:function(item){
my.item.suffix = function(suffix){
console.log(item, suffix);
}
}
};
(如果标题有误,很抱歉。我不确定术语)试试这个:
var my = {
thing: undefined,
// sets the `thing` property of `my`,
// then returns `my` for chaining
item: function (t) {
thing = t;
return this;
},
// concatenates the `thing` property with the given
// suffix and returns it as a string
suffix: function (s) {
return thing + ' ' + s;
}
};
my.item('apple').suffix("is awesome");
//--> apple is awesome
你想做什么?你真正想解决的根本问题是什么?为什么要使用对象文字?你展示的一行有什么问题?你的问题就是一个很好的例子,它被称为“流畅的界面”。还有一种方法:我想这就是答案!干杯@Mikean现在我知道它叫什么了!谢谢@gearsdigitalIt可能你知道答案,其他访客可能不知道。我该如何纠正?也许我可以把标题改成像。。如何使用对象文字符号编写“流畅接口”代码