Javascript 创建原型时对方法的引用
我觉得有点沮丧,但我不明白,甚至不知道JavaScript原型是否可行。 我想在创建伪类的原型时使用一个方法:Javascript 创建原型时对方法的引用,javascript,prototype,Javascript,Prototype,我觉得有点沮丧,但我不明白,甚至不知道JavaScript原型是否可行。 我想在创建伪类的原型时使用一个方法: var Class = function() {} Class.prototype = { a: function() { return 'ok' } , z: Class.prototype.a() // I tried with `this`/`constructor`/etc. } // TypeError: Object [object Ob
var Class = function() {}
Class.prototype = {
a: function() {
return 'ok'
}
, z: Class.prototype.a() // I tried with `this`/`constructor`/etc.
} // TypeError: Object [object Object] has no method 'a' the rest isn't evaluated
var test = new Class()
test.z
我知道我可以这样做,但我想知道我是否仍然可以,但是Class.prototype声明中的所有方法/属性:
var Class = function() {}
Class.prototype.a = function() {
return 'ok'
}
Class.prototype.z = Class.prototype.a()
var test = new Class()
test.z // "ok"
谢谢。不,你不能。就像在定义对象属性的语句结束之前不能引用任何对象属性一样:
var x = {
y: 10,
z: x.y + 5 // TypeError, cannot read property `y` of undefined
};
变量x没有声明的值,因为声明已被提升,但它的值在赋值表达式全部求值之前是未定义的。不,不能。就像在定义对象属性的语句结束之前不能引用任何对象属性一样:
var x = {
y: 10,
z: x.y + 5 // TypeError, cannot read property `y` of undefined
};
变量x没有声明的值,因为声明被挂起,但它的值在赋值表达式全部计算完毕之前是未定义的。可以。您可以这样分配类:
var Class = function() {
if (!Class.prototype.a){
var proto = Class.prototype;
proto.a = function() {
return 'ok';
};
proto.z = proto.a();
}
}
var test = new Class;
console.log(test.z); //=> "ok"
另一个选项是使用单例创建原型属性/方法:
var Class = function(){};
Class.prototype = function(){
function a(){return 'ok';}
return {a: a, z: a()};
}();
var test = new Class;
console.log(test.z); //=> "ok"
是的,你可以。您可以这样分配类:
var Class = function() {
if (!Class.prototype.a){
var proto = Class.prototype;
proto.a = function() {
return 'ok';
};
proto.z = proto.a();
}
}
var test = new Class;
console.log(test.z); //=> "ok"
另一个选项是使用单例创建原型属性/方法:
var Class = function(){};
Class.prototype = function(){
function a(){return 'ok';}
return {a: a, z: a()};
}();
var test = new Class;
console.log(test.z); //=> "ok"
如果只有一个附加属性要添加,则可以执行以下操作:
(Class.prototype = {
a: function() {
return 'ok'
}
}).z = Class.prototype.a();
或者可以采用这种方法,使用匿名函数作为临时构造函数:
Class.prototype = new function() {
this.a = function() {
return 'ok'
}
this.z = this.a()
}
如果只有一个附加属性要添加,则可以执行以下操作:
(Class.prototype = {
a: function() {
return 'ok'
}
}).z = Class.prototype.a();
或者可以采用这种方法,使用匿名函数作为临时构造函数:
Class.prototype = new function() {
this.a = function() {
return 'ok'
}
this.z = this.a()
}
第一:公认的答案是错误的。你可以 第二:见过下面的模式吗
Class.prototype = {
constructor: Class
};
如果要使用此属性访问当前对象,则构造函数属性是必需的
第三:如果不在属性函数中,则上下文是窗口,在严格模式下未定义,而不是对象
第四:这就是你想要的:
function Class() {}
Class.prototype = {
constructor: Class, // required!
a: function() {
return 'ok';
},
z: function() {
// 'this' is the object *in* the function
return this.a();
}
};
var o = new Class();
console.log(o.z()); // "ok"
第一:公认的答案是错误的。你可以 第二:见过下面的模式吗
Class.prototype = {
constructor: Class
};
如果要使用此属性访问当前对象,则构造函数属性是必需的
第三:如果不在属性函数中,则上下文是窗口,在严格模式下未定义,而不是对象
第四:这就是你想要的:
function Class() {}
Class.prototype = {
constructor: Class, // required!
a: function() {
return 'ok';
},
z: function() {
// 'this' is the object *in* the function
return this.a();
}
};
var o = new Class();
console.log(o.z()); // "ok"
我同意选择的答案是错误的。但是,您的解决方案没有显示“ok”,因为方法z没有返回任何内容,并且不是te OP所要求的。我同意所选答案是错误的。但是,您的解决方案没有显示“ok”,因为方法z不返回任何内容,并且不是te OP所要求的。