標籤:執行 .sh 全域 function his object lis 對象 name
// 1.直接調用,指向全域
console.log(this);
// 2.在普通函數裡調用,指向全域
function fn(){
console.log(this);
}
fn();
3.建構函式普通調用,指向全域(建構函式也是普通函數,可以正常執行)
function Student(){
this.name="zhangsan";
console.log(this);
}
Student();
// 4.建構函式通過new調用建立一個執行個體對象,指向這個執行個體對象
var x=0;
function Student(name,x){
this.name=name;
this.x=x;
console.log(this.x);
}
var zhangsan=new Student("zhangsan",1);
var lisi=new Student("lisi",2);
// 5.對象(json建立)裡面的方法調用,指向這個對象
var object1={
name:"zhangsan",
show:function(){
console.log(this);
}
}
object1.show();
// 6.對象(通過Object建立)裡面的方法調用,指向這個對象
var object2 =new Object();
object2.name="zhangsan";
object2.show=function(){
console.log(this);
}
object2.show();
// 7.對象(通過建構函式建立)裡面的方法調用,指向這個對象
function Student(){
this.name="zhangsan"
this.show=function(){
console.log(this);
}
}
var object3=new Student();
object3.show();
Java Script this指向的所有情況