Javascript 为什么我的函数工作不正常?

Javascript 为什么我的函数工作不正常?,javascript,Javascript,很抱歉没有给出一个明确的标题,因为我不知道为什么我的脚本不起作用 var all=[]; function People(name){ this.name=name; this.func=function(){alert(this.name)}; all.push(this); }; var person1=new People('Peter'); for(i=0;i<all.length;i++){ var newBtn=document.create

很抱歉没有给出一个明确的标题,因为我不知道为什么我的脚本不起作用

var all=[]; 
function People(name){
    this.name=name;
    this.func=function(){alert(this.name)}; 
    all.push(this);
};
var person1=new People('Peter');
for(i=0;i<all.length;i++){
    var newBtn=document.createElement('input');
    document.body.appendChild(newBtn);
    newBtn.type='button';
    newBtn.value=all[i].name;

    newBtn.onclick=all[i].func; // why doesn't is say "Peter" when I click the button ?
};
var all=[];
职能人员(姓名){
this.name=name;
this.func=function(){alert(this.name)};
全部。推(这个);
};
var person1=新人(“彼得”);

对于(i=0;i尝试将person对象推送到所有数组

var all=[]; 
function People(name){
    this.name=name;
    this.func=function(){alert(this.name)}; 
};


var person1=new People('Peter');

//push the person objects

all.push(person1);

尝试将person对象推送到所有数组中

var all=[]; 
function People(name){
    this.name=name;
    this.func=function(){alert(this.name)}; 
};


var person1=new People('Peter');

//push the person objects

all.push(person1);

单击按钮时,事件处理程序的上下文(
this
变量)将成为按钮本身。您只需将
console.log(this)
放入
func
中即可检查它 我建议使用以下代码:

for(i=0;i<all.length;i++){
    var newBtn=document.createElement('input');
    document.body.appendChild(newBtn);
    newBtn.type='button';
    newBtn.value=all[i].name;

    newBtn.onclick=all[i].func.bind(all[i]);
};

for(i=0;i

当您单击按钮时,事件处理程序的上下文(
此变量)将成为按钮本身。您只需将
控制台.log(此)
放入
func
中即可进行检查
我建议使用以下代码:

for(i=0;i<all.length;i++){
    var newBtn=document.createElement('input');
    document.body.appendChild(newBtn);
    newBtn.type='button';
    newBtn.value=all[i].name;

    newBtn.onclick=all[i].func.bind(all[i]);
};
用于行中的(i=0;i

this.func=function(){alert(this.name)}; 
将this.name替换为just name,因为您在范围外调用它,所以您的this是不同的(它是button-object HTMLInputElement)。

行中

this.func=function(){alert(this.name)}; 
将this.name替换为just name,因为您在范围外调用它,所以您的this是不同的(它是button-object HTMLInputElement)。

尝试一下:-

JS:-

var all=[]; 
function People(name){
    this.name=name;
    this.func=function(){alert(name)}; 
    all.push(this);
};
var person1=new People('Peter');
for(i=0;i<all.length;i++){
    var newBtn=document.createElement('input');
    document.body.appendChild(newBtn);
    newBtn.type='button';
    newBtn.value=all[i].name;

    newBtn.onclick=all[i].func; // why doesn't is say "Peter" when I click the button ?
};
var all=[];
职能人员(姓名){
this.name=name;
this.func=function(){alert(name)};
全部。推(这个);
};
var person1=新人(“彼得”);
对于(i=0;i请尝试以下方法:-

JS:-

var all=[]; 
function People(name){
    this.name=name;
    this.func=function(){alert(name)}; 
    all.push(this);
};
var person1=new People('Peter');
for(i=0;i<all.length;i++){
    var newBtn=document.createElement('input');
    document.body.appendChild(newBtn);
    newBtn.type='button';
    newBtn.value=all[i].name;

    newBtn.onclick=all[i].func; // why doesn't is say "Peter" when I click the button ?
};
var all=[];
职能人员(姓名){
this.name=name;
this.func=function(){alert(name)};
全部。推(这个);
};
var person1=新人(“彼得”);
对于(i=0;i请检查此代码

    $(document).ready(function(){
    var all=[]; 
    function People(name){
        this.name=name;
        this.func=function(){alert(name)}; 
        all.push(this);
    };
    var person1=new People('Peter');

    for(i=0;i<all.length;i++){
        var newBtn=document.createElement('input');
        newBtn.type='button';
        newBtn.value=all[i].name;
        newBtn.onclick=all[i].func;
        document.body.appendChild(newBtn);

    }
});
$(文档).ready(函数(){
var all=[];
职能人员(姓名){
this.name=name;
this.func=function(){alert(name)};
全部。推(这个);
};
var person1=新人(“彼得”);
对于(i=0;i请检查此代码

    $(document).ready(function(){
    var all=[]; 
    function People(name){
        this.name=name;
        this.func=function(){alert(name)}; 
        all.push(this);
    };
    var person1=new People('Peter');

    for(i=0;i<all.length;i++){
        var newBtn=document.createElement('input');
        newBtn.type='button';
        newBtn.value=all[i].name;
        newBtn.onclick=all[i].func;
        document.body.appendChild(newBtn);

    }
});
$(文档).ready(函数(){
var all=[];
职能人员(姓名){
this.name=name;
this.func=function(){alert(name)};
全部。推(这个);
};
var person1=新人(“彼得”);

对于(i=0;i@KirenSiva通常是一个好问题,但不需要大声说出来。@KirenSiva他没有说它抛出了一个错误,他说当他点击按钮时它不会提醒Peter。@KirenSiva通常是一个好问题,但不需要大声说出来。:)@KirenSiva他没有说它抛出了一个错误,他说当他点击按钮时,它没有提醒Peter。+1是的,我只是这么做了,添加了console.log(这个),来回答…你打败了我XD@ArtyomNeustroev谢谢,你的建议很有效。我将尝试用(name)替换(this.name)首先,正如其他人所建议的。如果我得到任何错误,我将仔细查看bind()+1是的,我刚刚做到了,添加了console.log(这个)来回答…而你打败了我XD@ArtyomNeustroev谢谢,你的建议很有效。我将尝试用(name)替换(this.name)首先,正如其他人所建议的,如果我得到任何错误,我将仔细查看bind()