Javascript 组合已具有自己属性的方法(使用prototype属性)
我有两个对象,human和sam,每个对象有两个给定的属性Javascript 组合已具有自己属性的方法(使用prototype属性),javascript,prototype,Javascript,Prototype,我有两个对象,human和sam,每个对象有两个给定的属性 var human = { legs: 2, group: "mammal" } var sam = { age: 23, married: true } 我是否可以将human对象附加到sam原型,以便将human的属性委托给sam,并且sam对象保持完整。所需结果: console.log(sam.legs);//=>2 我知道的一种方法(但我知道是“坏习惯”)是使用\uuu proto\uu属性: var s
var human = {
legs: 2,
group: "mammal"
}
var sam = {
age: 23,
married: true
}
我是否可以将human对象附加到sam原型,以便将human的属性委托给sam,并且sam对象保持完整。所需结果:
console.log(sam.legs);//=>2
我知道的一种方法(但我知道是“坏习惯”)是使用
\uuu proto\uu
属性:
var sam = {
age: 23,
married: true,
__proto__: human // *NOT IDEAL*
}
或
我知道Object.create(),但我不知道如何在不删除sam变量中已存储的所有内容的情况下实现它
我知道我可以先将人类对象附加到sam,然后将属性指定给sam: …但我的问题的重点是,如果我使用prototype属性附加两个已经有自己属性的对象?我可以用我不知道的方式使用Object.create()吗?
我一定已经阅读了google results for
object.create()
第一页上的所有内容,但我从来没有看到过正确类型的示例
编辑:我不想只是复制属性,因为我正在制作一个游戏,我不希望更改是永久性的。比如说,
sam
拥有human
原型,但在游戏中的任何时刻,他的原型都可能变成僵尸
或其他任何东西。你可以尝试构造函数,并将一个实例转换成其他东西
定义将人员或僵尸作为构造函数参数的人员和僵尸类型
function Human(obj){
obj = obj || {};
this.legs = obj.legs === 0 ? 0 :
obj.legs || 2;//defaults to 2
this.name = obj.name || 'nameless';
this.dead = false;
}
Human.prototype.saySomething = function(){
console.log('Hello, I\'m ' + this.name);
};
//other functions of Human
function Zombie(obj){
//Zombie is a Human but it's dead
// re use Human constructor
Human.call(this,obj);
//re set the parameters that differ from Human
this.dead = true;
//add Zombie specific parameters if needed
}
//inherit prototype from Human
Zombie.prototype = Object.create(Human.prototype);
//repair constructor
Zombie.prototype.constructor=Zombie;
//override saySomething
Zombie.prototype.saySomething = function(){
console.log('Huarrrghhh');
}
var ben = new Human({name:'ben'});
ben.saySomething();
console.log(ben);
// now ben becomes a Zombie
ben = new Zombie(ben);
ben.saySomething();
console.log(ben);
您可以尝试以下不同的factory功能:
Human.toZombie = function(human){
return new Zombie(human);
}
var ben = new Human();
ben = Human.toZombie(ben);
更多关于构造函数和原型的信息:这是关于原型的,但似乎不是关于prototype.js,即框架。你真的想包含这个标记吗?通常的javascript方法是将
human
的属性复制到sam
中,这样sam
就变成了一个属性并集的组合对象。也许你应该描述一下你有什么问题使得通常的方法不适用.\uuuu proto\uuuu
已弃用。有一个实验性的.setPrototypeOf()
,但您似乎需要解释为什么通常的属性复制(100%支持且安全)不适合您。
var human = {
legs: 2,
group: "mammal"
}
var sam = Object.create(human);
sam.age = 23;
married: true;
function Human(obj){
obj = obj || {};
this.legs = obj.legs === 0 ? 0 :
obj.legs || 2;//defaults to 2
this.name = obj.name || 'nameless';
this.dead = false;
}
Human.prototype.saySomething = function(){
console.log('Hello, I\'m ' + this.name);
};
//other functions of Human
function Zombie(obj){
//Zombie is a Human but it's dead
// re use Human constructor
Human.call(this,obj);
//re set the parameters that differ from Human
this.dead = true;
//add Zombie specific parameters if needed
}
//inherit prototype from Human
Zombie.prototype = Object.create(Human.prototype);
//repair constructor
Zombie.prototype.constructor=Zombie;
//override saySomething
Zombie.prototype.saySomething = function(){
console.log('Huarrrghhh');
}
var ben = new Human({name:'ben'});
ben.saySomething();
console.log(ben);
// now ben becomes a Zombie
ben = new Zombie(ben);
ben.saySomething();
console.log(ben);
Human.toZombie = function(human){
return new Zombie(human);
}
var ben = new Human();
ben = Human.toZombie(ben);