Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 使用对象方法显示对象属性值_Javascript_Arrays_Function_Object - Fatal编程技术网

Javascript 使用对象方法显示对象属性值

Javascript 使用对象方法显示对象属性值,javascript,arrays,function,object,Javascript,Arrays,Function,Object,我是JS新手,最近学习了对象和函数。这里我想为对象数组中的每个对象调用一个函数。我正在使用forEach这样做。结果是每个属性的未定义 函数showDetail(){ console.log(`name:${this.name} 价格:${this.price} 售出:${this.salled} 控制台:${this.console} `); } 康斯特游戏=[{ 名称:“撞车班迪科特N.理智三部曲”, 售价:1060, 售出:20, 控制台:“PS4”, }, { 名称:“乐高奇迹超级英雄

我是JS新手,最近学习了对象和函数。这里我想为对象数组中的每个对象调用一个函数。我正在使用
forEach
这样做。结果是每个属性的
未定义

函数showDetail(){
console.log(`name:${this.name}
价格:${this.price}
售出:${this.salled}
控制台:${this.console}
`);
}
康斯特游戏=[{
名称:“撞车班迪科特N.理智三部曲”,
售价:1060,
售出:20,
控制台:“PS4”,
},
{
名称:“乐高奇迹超级英雄”,
价格:700,
售出:25,
主机:“XBOX”,
showDetail:function(){
console.log(this.name);
}
},
{
名称:“Gta V”,
价格:1449,
售出:30,
控制台:“PS4”,
showDetail:function(){
console.log(this.name);
}
}
];

游戏。forEach(showDetail)您应该将“game”作为参数传递给函数并打印其属性,而不是从“this”

function showDetail(game){
   console.log(`name: ${game.name}
   price: ${game.price}
   sold: ${game.sold}`);
}

const games = [ ..... ]
games.forEach( function(game) { showDetail(game) });
//if there is support for arrow functions, it's cleaner
games.forEach( game => showDetail(game) );
//also, as mentioned by Máté in the comments
games.forEach( showDetail ); //will work
如果希望“showDetail”函数与此“this”一起使用,则应将“game”绑定到该函数

games.forEach( game => showDetail.bind(game)() );
function showDetail(){
   //here the 'this' is the 'game', because it has been bounded via .bind() method
}

您应该将“game”作为参数传递给函数并打印其属性,而不是从“this”

function showDetail(game){
   console.log(`name: ${game.name}
   price: ${game.price}
   sold: ${game.sold}`);
}

const games = [ ..... ]
games.forEach( function(game) { showDetail(game) });
//if there is support for arrow functions, it's cleaner
games.forEach( game => showDetail(game) );
//also, as mentioned by Máté in the comments
games.forEach( showDetail ); //will work
如果希望“showDetail”函数与此“this”一起使用,则应将“game”绑定到该函数

games.forEach( game => showDetail.bind(game)() );
function showDetail(){
   //here the 'this' is the 'game', because it has been bounded via .bind() method
}
当你写作时

games.forEach(showDetail);
games.forEach(game => {
   showDetail.call(game);
});
showDetails是forEach的回调函数,它作为第一个参数传递给对象,因此您可以编写

function showDetail(game) {
  console.log(`name: ${game.name}
         price: ${game.price}
         sold: ${game.sold}
         console: ${game.console}
         `);
}
当您在showDetail中
this
时,this值未绑定到对象上下文,因此
this.name
不会返回对象值。但是如果你写

games.forEach(showDetail);
games.forEach(game => {
   showDetail.call(game);
});
您正在提供要成为对象的上下文,在编写时,showdeail中的这个案例
this.name
将起作用

games.forEach(showDetail);
games.forEach(game => {
   showDetail.call(game);
});
showDetails是forEach的回调函数,它作为第一个参数传递给对象,因此您可以编写

function showDetail(game) {
  console.log(`name: ${game.name}
         price: ${game.price}
         sold: ${game.sold}
         console: ${game.console}
         `);
}
当您在showDetail中
this
时,this值未绑定到对象上下文,因此
this.name
不会返回对象值。但是如果你写

games.forEach(showDetail);
games.forEach(game => {
   showDetail.call(game);
});
您正在提供要成为对象的上下文,而showdeail中的这个案例
this.name
将起作用

也许您的意思是这样的

//构造函数
功能游戏(parms){
this.name=parms.name;
this.price=parms.price;
this.selled=parms.selled;
this.gconsole=parms.gconsole;
}
//toString覆盖
Game.prototype.toString=函数(){
返回`name:${this.name}
价格:${this.price}
售出:${this.salled}
gconsole:${this.gconsole}
`;
}
康斯特游戏=[
新游戏({
名称:“撞车班迪科特N.理智三部曲”,
售价:1060,
售出:20,
gconsole:'PS4'
}),
新游戏({
名称:“乐高奇迹超级英雄”,
价格:700,
售出:25,
gconsole:'XBOX'
}),
新游戏({
名称:“Gta V”,
价格:1449,
售出:30,
gconsole:'PS4'
})
];
游戏。forEach(函数(g){
log(g.toString());
});也许你是这个意思

//构造函数
功能游戏(parms){
this.name=parms.name;
this.price=parms.price;
this.selled=parms.selled;
this.gconsole=parms.gconsole;
}
//toString覆盖
Game.prototype.toString=函数(){
返回`name:${this.name}
价格:${this.price}
售出:${this.salled}
gconsole:${this.gconsole}
`;
}
康斯特游戏=[
新游戏({
名称:“撞车班迪科特N.理智三部曲”,
售价:1060,
售出:20,
gconsole:'PS4'
}),
新游戏({
名称:“乐高奇迹超级英雄”,
价格:700,
售出:25,
gconsole:'XBOX'
}),
新游戏({
名称:“Gta V”,
价格:1449,
售出:30,
gconsole:'PS4'
})
];
游戏。forEach(函数(g){
log(g.toString());

});
games.forEach(函数(游戏){showDetail(游戏)})太过分了<代码>games.forEach(showDetail)工作正常,因为
showdeail()
会将
游戏作为参数接收。至少在这种情况下,当参数可以按原样传递时。谢谢@MátéSafranka,我不习惯这种方法,我通常会公开参数。我不记得你提到的这种方法会起作用。我已经更新了我的答案。是的,我也对我的评论进行了一些编辑以澄清。更常见的情况是,在将参数传递到回调之前,我们需要以某种方式转换参数,因此,如果OP也看到该选项,也不会有任何伤害。@MátéSafranka,谢谢。为什么
这个
不起作用
Foreach
正在对象数组中迭代,函数应该知道它是一个对象。因为函数不是作为对象的方法调用的。您可能把它与事件处理程序混淆了。
games.forEach(函数(游戏){showtail(游戏)})太过分了<代码>games.forEach(showDetail)工作正常,因为
showdeail()
会将
游戏作为参数接收。至少在这种情况下,当参数可以按原样传递时。谢谢@MátéSafranka,我不习惯这种方法,我通常会公开参数。我不记得你提到的这种方法会起作用。我已经更新了我的答案。是的,我也对我的评论进行了一些编辑以澄清。更常见的情况是,在将参数传递到回调之前,我们需要以某种方式转换参数,因此,如果OP也看到该选项,也不会有任何伤害。@MátéSafranka,谢谢。为什么
这个
不起作用
Foreach
正在对象数组中迭代,函数应该知道它是一个对象。因为函数不是作为对象的方法调用的。您可能把它与事件处理程序混淆了。要将游戏绑定为上下文,您可以执行
games.forEach(i=>{showtail.bind(i))
@ChrisG:没有理由那样使用
.bind()
。如果要立即调用该函数,请使用
.cal