Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在[array]中设置未声明的{object}的原型属性_Javascript - Fatal编程技术网

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
是什么?它似乎被称为巢外对象。
应该是
这个
(我想是吧?)