Node.js(七)——HTTP範圍、上下文

來源:互聯網
上載者:User

標籤:node.js(七)——http範圍、上下文

http源碼解讀


什麼是範圍?

範圍和調用函數訪問變數的能力有關

範圍分局部範圍和全域範圍,同時範圍往往和變數有關係,

處在局部範圍裡面可以訪問到全域範圍的變數,反之則訪問不到

執行個體如下:

var globalVariable = ‘This is global variable‘function globalFunction(){var localVariable =‘This is local variable‘console.log(‘Visit global/local variable‘)console.log(globalVariable)console.log(localVariable)globalVariable = ‘This is changed variable‘console.log(globalVariable)function localFunction(){var innerLocalVariable = ‘This is inner local variable‘console.log(‘Visit global/local/inner variable‘)console.log(globalVariable)console.log(localVariable)console.log(innerLocalVariable)}localFunction()}globalFunction()

執行結果如下:

650) this.width=650;" src="https://s2.51cto.com/wyfs02/M02/8F/0F/wKioL1jSfBywadOtAABMq_zzvIc735.jpg" title="36020170315204858576.jpg" alt="wKioL1jSfBywadOtAABMq_zzvIc735.jpg" />

什麼是上下文?

this關鍵字有關,是調用當前可執行代碼的引用

常常代表this變數的值以及它的指向,它決定一個函數怎麼被調用,當這個函數作為一個對象的方法被調用時this總是指向調用這個方法的對象

代碼如下:

第一種

var pet = {words:‘...‘,speak:function(){console.log(this.words)console.log(this === pet)}}pet.speak()

執行結果如下:

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M02/8F/11/wKiom1jSfT2xvtPSAAALSnZZEN8956.jpg" title="36020170315204858576.jpg" alt="wKiom1jSfT2xvtPSAAALSnZZEN8956.jpg" />


第二種:

function pet(words){this.words = wordsconsole.log(this.words)//這個this指向頂層的global對象console.log(this ===  global)}pet(‘...‘)

執行結果如下:

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M02/8F/11/wKiom1jSfT2xvtPSAAALSnZZEN8956.jpg" title="36020170315204858576.jpg" alt="wKiom1jSfT2xvtPSAAALSnZZEN8956.jpg" />


第三種:

function Pet(words){this.words = wordsthis.speak = function(){console.log(this.words)//此時的this指向的誰?cat對象console.log(this)}}var cat = new Pet(‘Miao‘)cat.speak()

執行結果如下:

650) this.width=650;" src="https://s2.51cto.com/wyfs02/M02/8F/11/wKiom1jSfoeAYJnRAAAXulZT3Gs068.jpg" title="36020170315204858576.jpg" alt="wKiom1jSfoeAYJnRAAAXulZT3Gs068.jpg" />


提出上下文是為了引出函數所擁有的兩個方法:call和apply,可以改變上下文執行對象,可以在自訂上下文中執行函數,作用是一樣的

只是用法有區別

call函數需要一個參數列表,apply允許你傳遞一個參數作為數組,具體的是作用呢是調用一個對象的方法以另一個對象替換當前對象,其實就是更改當前對象的this指向的內容,標準一點就是:以某個方法當做指定某個對象的方法被執行

樣本如下:

var pet = {words:‘...‘,speak:function(say){console.log(say+‘ ‘+this.words)}}//pet.speak(‘Speak‘)var dog = {words:‘Wang‘}//讓dog也能有speak方法pet.speak.call(dog,‘Speak‘)

運行結果如下:

650) this.width=650;" src="https://s1.51cto.com/wyfs02/M02/8F/0F/wKioL1jSgkzC9SrIAAAN7sAwmfA508.jpg" title="36020170315204858576.jpg" alt="wKioL1jSgkzC9SrIAAAN7sAwmfA508.jpg" />


利用call和apply可以方便的實現繼承

function Pet(words){this.words = wordsthis.speak = function(){console.log(this.words)}}function Dog(words){Pet.call(this,words)//Pet.apply(this,arguments)}//通過new產生執行個體對象var dog = new Dog(‘Wang‘)dog.speak()

運行結果如下:

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/8F/0F/wKioL1jSgymjixJyAAANqa1Jr4A060.jpg" title="36020170315204858576.jpg" alt="wKioL1jSgymjixJyAAANqa1Jr4A060.jpg" />


這就是call和apply的用法

本文出自 “IT菜鳥” 部落格,請務必保留此出處http://mazongfei.blog.51cto.com/3174958/1909396

Node.js(七)——HTTP範圍、上下文

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.