JavaScript中this的9種應用情境及三種複合應用情境,javascript9種

來源:互聯網
上載者:User

JavaScript中this的9種應用情境及三種複合應用情境,javascript9種

【情境1】全域環境中的this指向全域對象

this.a = 10;alert(a);//10b = 20;alert(this.b);//20var c = 30;alert(this.c);//30

【情境2】對象內建函式的this指向調用函數的當前對象

var a = 10;var bar = { a: 20, test: function(){  alert(this.a); }}bar.test();//20

【情境3】全域環境函數的this指向全域對象

var a = 10;function foo(){ alert(this.a);}foo();//10

【情境4】匿名函數中的this指向全域對象

var a = 10;var foo = { a: 20, fn: (function(){  alert(this.a); })()}foo.fn//10

【情境5】setInterval和setTimeout定時器中的this指向全域對象

var a = 10;var oTimer1 = setInterval(function(){ var a = 20; alert(this.a);//10 clearInterval(oTimer1);},100);

【情境6】eval中的this指向調用上下文中的this

(function(){ eval("alert(this)");//[object Window]})();function Foo(){ this.bar = function(){  eval("alert(this)");//[object Object] }}var foo = new Foo();foo.bar();

【情境7】建構函式中的this指向構造出的新對象

function Person(name,age){ this.name = name; this.age = age; this.sayName = function(){  alert(this.name); }}var p1 = new Person('lily','20');p1.sayName();//'lily'

【情境8】new Function中的this指向全域對象

(function(){ var f = new Function("alert(this)"); f();//[object Window]})();function Foo(){ this.bar = function(){  var f = new Function("alert(this)");  f();//[object Window] }}var foo = new Foo();foo.bar();

【情境9】apply和call中的this指向參數中的對象

var a = 10;var foo = { a: 20, fn: function(){  alert(this.a); }};var bar ={ a: 30}foo.fn.apply();//10(若參數為空白,預設指向全域對象)foo.fn.apply(foo);//20foo.fn.apply(bar);//30

【複合情境1】

var someone = { name: "Bob", showName: function(){  alert(this.name); }};var other = { name: "Tom", showName: someone.showName}other.showName();  //Tom//以上函數相當於var other = { name: "Tom", showName: function(){  alert(this.name); }}other.showName();  //Tom

【複合情境2】

var name = 2;var a = { name: 3, fn: (function(){  alert(this.name); })(), fn1:function(){  alert(this.name); }}a.fn;//2[匿名函數中的this指向全域對象]a.fn1();//3[對象內建函式的this指向調用函數的當前對象]

【複合情境3】

var name = "Bob"; var nameObj ={  name : "Tom",  showName : function(){  alert(this.name); },  waitShowName : function(){  var that = this;  setTimeout(function(){   that.showName();  }, 1000); }}; nameObj.waitShowName();//"Tom"[that=this改變this的指向,使this從指向全域變數變化到指向nameObj]var name = "Bob"; var nameObj ={  name : "Tom",  showName : function(){   alert(this.name);  },  waitShowName : function(){  var that = this;//that指向nameObj  setTimeout(function(){   (function(){     alert(this.name);   })();  }, 1000); }}; nameObj.waitShowName();// 'Bob'[形成匿名函數,this指向全域變數] 

本文給大家介紹了this中的9種應用情境,希望對大家有所協助,本站每天都有新的內容更新,請廣大網友持續關注本站,謝謝。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.