国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

javascript - Issues with this and constructors in JS
我想大聲告訴你
我想大聲告訴你 2017-06-26 10:53:14
0
5
889
function People() {
}
People.prototype.say = function () {
    alert("hello");
}

function Student() {
}
Student.prototype = new People();
var superSay = Student.prototype.say;

Student.prototype.say = function () {
    superSay.call(this);            // 為什么會是"hello"?
    alert("stu-hello");
}
var s = new Student();
s.say();

As commented in the code, superSay.call(this)Why is the People.prototype.say function called? Who does this point to?

我想大聲告訴你
我想大聲告訴你

reply all(5)
曾經(jīng)蠟筆沒有小新

this points to the Student{} class. You can verify this by adding a line console.log(this) above superSay.call(this).

Then, let’s look at this code

Student.prototype = new People();

For the convenience of explanation later, I call the instance created by new People() here Instance X.
Because superSay = Student.prototype.say, because the above Student.prototype = new People();, so Student.prototype is instance X.
So actually superSay calls say of instance X, not People.prototype.say.

As for why you think you are calling People.prototype.say, it is mainly a problem with the prototype chain. Instance X is an instance of the People class, so all methods of Instance So Instance X.sayIf the say method is not overridden for Instance In addition, the call in superSay.call(this)just changes the context of this. However, since

superSay

is instance

迷茫

This problem is often encountered in js

代言

Personal opinion: Let’s first talk about the search sequence for finding the say method: s——student.prototype——people.prototype——Object. If found, it is ready and stop searching. Combined with your code, say will only find student.prototype. Here you can first change your code to look like this to make it clearer:

    People.prototype.say = function () {
        console.log(123);
    }
    People.prototype.jiao = function () {
        console.log(456);
    }
    Student.prototype.say = function () {
          console.log(321)
    }

Others remain unchanged. At this time, s.say() - outputs 321, s.jiao() - outputs 456. Then go back to your code, because you have overridden the sdudent.prototype.say method, so this code will be executed

    Student.prototype.say = function () {
        superSay.call(this);            // 為什么會是"hello"?
        alert("stu-hello");
    }

The first sentence superSay.call(this), first superSay is a variable, the variable type is function, you can add a piece of code console.log(typeof supperSay) after var superSay = Student.prototype.say, so you just Call this function, and this variable stores Student.prototype.say. Execute to var superSay = Student.prototype.say. In fact, the value assigned here is People.prototype.say. This point is similar on pages 166-167 of Elevation. Regarding this point, you can change these two sections of your code to this, leaving the rest unchanged.

    var superSay = Student.prototype.say;
    Student.prototype = new People();

An error will be reported when calling superSay at this time, because when var superSay = Student.prototype.say is executed; student.prototype has only one constructer attribute and no say method. Then go back to your code and People.prototype.say will be assigned to superSay

綜上第一句代碼會輸出hello,而你的call根本沒什么用
第二句就不用說了。
關(guān)于this其實我也不是特別清楚。我原來還認為console.log(this)會輸出S,結(jié)果輸出student,還需繼續(xù)學習。
我想大聲告訴你

Bind this in the superSay constructor to Student.prototype.

洪濤

Aren’t you afraid that the poster will be fooled by all the talk upstairs?

In his program, stuSay points to the anonymous function object function () {alert("hello");}. As long as you don't reassign stuSay, it will always point to this function object. No matter where you call it, it will always be like this. As a result, as for who this points to inside the function body, please read the Rhinoceros book...

The poster will not have these problems after reading the function part of the Rhinoceros book. As for the so much nonsense upstairs, don’t make the poster dizzy

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template