this執(zhí)行全局環(huán)境中 this 指向 window
this很重要的解析
https://segmentfault.com/q/1010000004563202
匿名函數(shù)中this 指向 window
function (){
this --->window
}
var Temp = function(){
this->window;
}
在一個對象中封裝了一個函數(shù)午绳,這個函數(shù)this 指向 這個對象
var ?object = {
name:"ZZZ",
obj :function(){
this->object;
}
}
還有就是先創(chuàng)建一個對象 var object;
object .a="kkk";
object.b=function(){
this->obj;
}
//注意上下區(qū)別
new 一個實例挖帘,this 指向那個獲得實例的對象
function B(){
? ? ? ?name ="BO";
? ? ? ? age=19;
? ? ? ? ? function C(){
? ? ? ? ? ? ? ? ? ? ? console.log("HELLO");
? ? ? ? ? ? ?}
? ? ? ?C();//調用C函數(shù)
}
B.name; //B,函數(shù)名 .name 是函數(shù)名本身,不是內部屬性的name 字段
B.內部屬性是錯誤的娄周,B內部屬性B函數(shù)執(zhí)行完就會銷毀,
外部是訪問不到的起宽;
實例只能訪問到原型鏈上的屬性躬络,訪問不到內部屬性
var T = new B();//B的this ->T輸出 HELLO(內部調用了C()函數(shù))
T.name//undefine?
T不能訪問到name,age ,C();
B.prototype.getName=function(){
? ? ? ? ? ? ?console.log(this.name);
}
B.getName();//ERROR
T.name="TT";
T.getName();//TT;
T.Age =999;
B.prototype.Age=333;
T.Age;//
333?999
T._proto_//object?
T._proto_.construct //function B();