【发布时间】:2014-08-17 18:31:34
【问题描述】:
我刚刚开始学习 javascript 中的原型,但我无法理解我的代码中存在什么问题。对不起,我的问题可能看起来很傻
我收到这样的错误:
Uncaught TypeError: undefined is not a function
为什么是未定义的?我继承了用户的功能。 看不懂:(
var user = {
sayName: function() {
console.log(this.name)
}
};
user.name = "viktor";
user.sayName();
var user2 = {};
user2.prototype = user;
user2.name = "segey";
user2.sayName();
【问题讨论】:
-
var user2 = Object.create(user) -
prototype属性与构造函数functions 相关联,以便在创建new实例时使用,而不是与实例本身相关联。 -
.prototype属性仅与函数真正相关。该函数在使用new调用时会建立正在创建的新对象和函数的.prototype对象之间的关系。 ...就像乔南森说的那样^^^
标签: javascript