Some questions about call ()?

help explain why the output is 333 and aaa

function superClass () {

this.c =333;
this.d ="bbb";

}

function subClass () {

this.c =222;
superClass.call(this);
this.d ="aaa";

console.log(this.c);//333
console.log(this.d);//aaa

}

subClass ();

Mar.11,2021

the this is all window, in the browser environment. This code is equivalent to modifying global variables in the order in which they are executed, and has nothing to do with call.


  1. subClass (); is executed in the global environment, where the this points to window;
  2. superClass.call (this) here pass window to the function, and the this in it also points to window;
  3. after code execution is equivalent to
window.c =222;
window.c =333;
window.d ='bbb';
window.d ='aaa';

console.log(window.c);//333
console.log(window.d);//aaa

one. Simple statement about this: ignore the es6 arrow function and strict mode. Whoever calls this, points to him. For example:

var a = 2;
var obj = {
    a:1,
    get:function(){
        console.log(this.a);
    }
};
obj.get();//1
var fun = obj.get;
fun();//2
//thiswindowfunwindow,x(),thiswindows
//x.y(),thisx
//x.y.z(),thisy

two. Call/apply usage: call is similar to apply. I use call as an example

x.call(y,z);//y.x(z)thisy

conclusion, such as the above two statements. Globally, this, in the browser means that both window.superClass (), c and d are tied to window, and the result is like the main result of the question.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b389d8-4d7c7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b389d8-4d7c7.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?