JavaScript超级属性
JavaScript uber property
我期待我的 console.log 打印 "Creature and Charles" 但是它只打印生物:
function creature() {}
creature.prototype.name = 'Creature';
creature.prototype.showName = function () {
return this.constructor.uber ? this.constructor.uber.toString() + ',' + this.name : this.name;
}
function dog() {}
var F = function () {}
dog.prototype = new F();
dog.prototype.constructor = dog;
dog.prototype.name = 'Charles';
dog.uber = creature.prototype;
dog.prototype = creature.prototype;
var cat = new dog();
console.log(cat.showName());
有什么帮助吗?
我建议您使用 dog
原型进行所有操作稍微简单一点:
function creature() {}
creature.prototype.name = 'Creature';
creature.prototype.showName = function () {
return this.constructor.uber ? this.constructor.uber.showName() +
',' + this.name : this.name;
}
function dog() {}
dog.prototype = Object.create(creature.prototype);
dog.prototype.constructor = dog;
dog.prototype.name = 'Charles';
dog.uber = creature.prototype;
var cat = new dog();
console.log(cat.showName());
我期待我的 console.log 打印 "Creature and Charles" 但是它只打印生物:
function creature() {}
creature.prototype.name = 'Creature';
creature.prototype.showName = function () {
return this.constructor.uber ? this.constructor.uber.toString() + ',' + this.name : this.name;
}
function dog() {}
var F = function () {}
dog.prototype = new F();
dog.prototype.constructor = dog;
dog.prototype.name = 'Charles';
dog.uber = creature.prototype;
dog.prototype = creature.prototype;
var cat = new dog();
console.log(cat.showName());
有什么帮助吗?
我建议您使用 dog
原型进行所有操作稍微简单一点:
function creature() {}
creature.prototype.name = 'Creature';
creature.prototype.showName = function () {
return this.constructor.uber ? this.constructor.uber.showName() +
',' + this.name : this.name;
}
function dog() {}
dog.prototype = Object.create(creature.prototype);
dog.prototype.constructor = dog;
dog.prototype.name = 'Charles';
dog.uber = creature.prototype;
var cat = new dog();
console.log(cat.showName());