Javascript 在[array]中设置未声明的{object}的原型属性
我有一个数组Javascript 在[array]中设置未声明的{object}的原型属性,javascript,Javascript,我有一个数组 var Nest = []; 我打算用这样的东西填满它 bird_id1 = /*gotten from some outer variable, say*/ 8; bird_id2 = 9; bird1 = Nest[bird_id1] = { id: bird_id1, ... } bird2 = Nest[bird_id2] = { id: bird_id2, ... } 现在我想知道我是否可以让bird,或者=Nest[bird\u idX]通过prototype设置
var Nest = [];
我打算用这样的东西填满它
bird_id1 = /*gotten from some outer variable, say*/ 8;
bird_id2 = 9;
bird1 = Nest[bird_id1] = { id: bird_id1, ... }
bird2 = Nest[bird_id2] = { id: bird_id2, ... }
现在我想知道我是否可以让bird
,或者=Nest[bird\u idX]
通过prototype设置一个预定义的函数集,这样我就可以像这样调用它了
bird1.chirp(this.id);
或
因此本质上,必须将chirp()
函数定义为{Nest[]}
对象(位于数组中)上的protptype。这怎么可能呢
我会尝试定义原型属性的常规方法
Nest[?].prototype = function chirp(){...}
但我不确定最好的方法是如何在原型中创建具有该功能的对象鸟,然后使用此对象实例填充数组:
var Bird = function(){};
Bird.prototype.chirp = function(id){};
var bird_id = 9;
bird1 = new Bird();
bird1.id = bird_id;
var Nest = [];
Nest[ bird_id ] = bird1;
然后,您可以轻松使用啁啾功能:
Nest[ bird_id ].chirp();
最好的方法是在原型中创建一个具有该功能的对象Bird,然后使用此对象实例填充数组:
var Bird = function(){};
Bird.prototype.chirp = function(id){};
var bird_id = 9;
bird1 = new Bird();
bird1.id = bird_id;
var Nest = [];
Nest[ bird_id ] = bird1;
然后,您可以轻松使用啁啾功能:
Nest[ bird_id ].chirp();
您可以创建
Bird
构造函数:
function Bird(id) {
this.id = id;
}
Bird.prototype.chirp = function () { /*chirpchirp*/ }
// subsequently
bird_id1 = /*gotten from some outer variable, say*/ 8;
bird_id2 = 9;
bird1 = Nest[bird_id1] = new Bird(bird_id1);
bird2 = Nest[bird_id2] = new Bird(bird_id2);
// constructor
function Bird(id) {
this.id = id;
}
// properties shared by all birds
Bird.prototype.chirp = function() {
console.log('My id is: ' + this.id);
}
// nest
var Nest = [];
// ids
var bird_id_1 = 8, bird_id_2 = 9;
// create birds
Nest[bird_id_1] = new Bird(bird_id_1);
Nest[bird_id_2] = new Bird(bird_id_2);
// make them sing
Nest.forEach(function(bird){ bird.chirp(); });
您可以创建
Bird
构造函数:
function Bird(id) {
this.id = id;
}
Bird.prototype.chirp = function () { /*chirpchirp*/ }
// subsequently
bird_id1 = /*gotten from some outer variable, say*/ 8;
bird_id2 = 9;
bird1 = Nest[bird_id1] = new Bird(bird_id1);
bird2 = Nest[bird_id2] = new Bird(bird_id2);
// constructor
function Bird(id) {
this.id = id;
}
// properties shared by all birds
Bird.prototype.chirp = function() {
console.log('My id is: ' + this.id);
}
// nest
var Nest = [];
// ids
var bird_id_1 = 8, bird_id_2 = 9;
// create birds
Nest[bird_id_1] = new Bird(bird_id_1);
Nest[bird_id_2] = new Bird(bird_id_2);
// make them sing
Nest.forEach(function(bird){ bird.chirp(); });
您可以使用构造函数创建鸟:
function Bird(id) {
this.id = id;
}
Bird.prototype.chirp = function () { /*chirpchirp*/ }
// subsequently
bird_id1 = /*gotten from some outer variable, say*/ 8;
bird_id2 = 9;
bird1 = Nest[bird_id1] = new Bird(bird_id1);
bird2 = Nest[bird_id2] = new Bird(bird_id2);
// constructor
function Bird(id) {
this.id = id;
}
// properties shared by all birds
Bird.prototype.chirp = function() {
console.log('My id is: ' + this.id);
}
// nest
var Nest = [];
// ids
var bird_id_1 = 8, bird_id_2 = 9;
// create birds
Nest[bird_id_1] = new Bird(bird_id_1);
Nest[bird_id_2] = new Bird(bird_id_2);
// make them sing
Nest.forEach(function(bird){ bird.chirp(); });
请注意,虽然每只鸟都有相同的
chirp
方法,但该方法不需要id参数来显示不同的内容。它只作用于bird的数据,在本例中记录调用它的bird的id。您可以使用构造函数创建bird:
function Bird(id) {
this.id = id;
}
Bird.prototype.chirp = function () { /*chirpchirp*/ }
// subsequently
bird_id1 = /*gotten from some outer variable, say*/ 8;
bird_id2 = 9;
bird1 = Nest[bird_id1] = new Bird(bird_id1);
bird2 = Nest[bird_id2] = new Bird(bird_id2);
// constructor
function Bird(id) {
this.id = id;
}
// properties shared by all birds
Bird.prototype.chirp = function() {
console.log('My id is: ' + this.id);
}
// nest
var Nest = [];
// ids
var bird_id_1 = 8, bird_id_2 = 9;
// create birds
Nest[bird_id_1] = new Bird(bird_id_1);
Nest[bird_id_2] = new Bird(bird_id_2);
// make them sing
Nest.forEach(function(bird){ bird.chirp(); });
请注意,虽然每只鸟都有相同的
chirp
方法,但该方法不需要id参数来显示不同的内容。它只作用于bird的数据,在本例中记录调用它的bird的id。它必须是数组,也可以是对象?像var Nest={}
然后Nest.chirp=function(){}
这样你的Nest[bird_id1]={}
仍然可以工作。@DontVoteMeDown它可以,但是Nest.chirp(this.id)
如何工作呢this.id
对于每只bird
(这就是我认为应该将Nest设置为数组的原因)@DontVoteMeDown因此不可能在数组中设置对象的原型。但是this
是什么?它似乎在一个嵌套对象之外被调用。bird
应该是this
(我想?)它一定是一个数组,或者可以是一个对象?像var Nest={}
然后Nest.chirp=function(){}
这样你的Nest[bird_id1]={}
仍然可以工作。@DontVoteMeDown它可以,但是Nest.chirp(this.id)
如何工作呢this.id
对于每只bird
(这就是我认为应该将Nest设置为数组的原因)@DontVoteMeDown因此不可能在数组中设置对象的原型。但是this
是什么?它似乎被称为巢外对象。鸟
应该是这个
(我想是吧?)