简体   繁体   English

当使用function.apply(this,arguments)时,参数的顺序会混合

[英]When using function.apply(this, arguments) order of arguments gets mixed

So I was trying to 'inherit' parameters in someway and I came across the use of 所以我试图在某种程度上'继承'参数,我遇到了使用

function.apply(this, arguments)

And it did the job... well for the most part. 它完成了这项工作......大部分都是如此。 What I am getting is that when I'm calling a function from the parent object the arguments of the parent function get in front of the others and when I call a function from the inheritor they don't. 我得到的是,当我从父对象调用一个函数时,父函数的参数在其他函数前面,当我从继承者调用函数时,它们不会。

Here's my code: 这是我的代码:

function Human(name, gender, tel, address){
    this.name = name;
    this.gender = gender;
    this.address = address;
    this.tel = tel;
}

Human.prototype.introduce = function(){
    console.log("Hi I'm " + this.name + " a " + this.gender + " from " + this.address + 
    ". My number is " + this.tel);
}

    Student.prototype = new Human; 
    Student.prototype.constructor = Student;


function Student(school,marks){
    Human.apply(this, arguments);
    this.school = school;
    this.marks = marks;

}

Student.prototype.average = function(){
    this.total = 0;
    this.average = 0;
    this.markslength = this.marks.length;
    for(var i = 0; i<this.markslength; i++){
        this.total = this.total + this.marks[i]
    }
    this.average = (this.total)/(this.markslength);

    var marks3 = [6,6,2]
    var Nasko = new Student('Nasko', 'Male', 14421687, 'Sofia', 'FELS', marks3);

And when I do: console.log(Nasko.name); 当我这样做: console.log(Nasko.name); It's OK. 没关系。 But when I do console.log(Nasko.average()); 但是当我做console.log(Nasko.average()); It gives me NaN. 它给了我NaN。 So my question is - how to actually 'fix' it 所以我的问题是 - 如何实际'修复'它

Sorry if I asked an already asked question but I couldn't really think of how to ask it in any short a redirection to another similar post will be welcome. 对不起,如果我问了一个已经被问过的问题,但我真的想不出怎么问它,任何简短的重定向到另一个类似的帖子将是受欢迎的。 Thanks in advance. 提前致谢。

You are using first two arguments for school and marks which actually name and gender. 你正在使用前两个学校的论点和标记,实际上是名称和性别。

You can use arguments for reading last values 您可以使用参数来读取最后的值

function Student(){
    Human.apply(this, arguments);
     var len = arguments.length;
    this.school = arguments[arguments.length-2]; // second last argument is school
    this.marks =  arguments[arguments.length-1]; // last argument is mark.

}

The value of two parameters named "school" and "marks" in the "Student" constructor will be "Nasko" and "Male" and in the "Human" class you are passing "Nasko" as "name" and "Male" as "gender" . "Student"构造函数中名为"school""marks"的两个参数的值将为“Nasko”和“Male”,而在"Human"类中,您将“Nasko”作为"name"和“Male”传递为"gender" I think you are confused with the apply and passing arguments to it a little bit. 我认为你对apply和传递参数感到困惑。 Of course after initializing the code marks is a string value of "Male" and you cannot have the average result. 当然,在初始化代码标记后,字符串值为“男性”,您无法获得平均结果。

Hope it helps. 希望能帮助到你。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM