我有一个像下面的构造函数
var Example = (function () {
function Example(opt) {
this.opt = opt;
return{
function(){ console.log(this.check()); } // Here is an Error
}
}
Example.prototype.check = function () {
console.infor('123');
};
return Example;
}) ();
var ex = new Example({ a:1 });
我知道我做错了但无法弄清楚这样做的方法。我想在对象里面使用prototype方法返回。请帮帮我。
答案 0 :(得分:2)
如果您想在构建实例时运行check()
,为什么不在构造函数中调用它?
var Example = (function () {
function Example(opt) {
this.opt = opt;
this.check(); //this gets called when instances are made
}
Example.prototype.check = function () {
console.infor('123');
};
return Example;
}) ();
//so `ex` is an instance of the Example Constructor
//and check gets called when you build it
var ex = new Example({ a:1 });
答案 1 :(得分:1)
看看
function Example(opt) {
this.opt = opt;
this.check()
}
Example.prototype.check = function () {
console.info('123');
};