【发布时间】:2017-01-21 15:04:34
【问题描述】:
当我以这种方式定义一个带有方法和子方法的 javascript 类时:
function Controller () {
this.OrdersSyncFreq = 3000; //ms
this.syncOrders = function () {};
this.syncOrders.start = function () { console.log("start was called"); };
this.syncOrders.stop = function () { console.log("stop was called"); };
}
但是我以后如何使用“原型”定义函数Controller.syncOrders.start()?这样的事情不起作用:
Controller.prototype.syncOrders.stop = function () {
console.log("The NEW stop was called");
}
【问题讨论】:
-
不,这从来没有真正奏效过,你不能在那些“方法”中使用
this。只是不要这样做。使用常规前缀。 -
这就是我在自己的答案尝试中使用 .bind(this) 的原因。 “常规前缀”是什么意思?
-
只需调用方法
.syncOrdersStop和.syncOrdersStart(或带下划线)。 -
是的,但这是我在自己的答案中推荐的结论类型!你有“-1”
标签: javascript class methods prototype