this | inherit

this | 變量 | 繼承

bind 設(shè)置 this


var name = 'global'
var App = {
  name: 'app',
  sayName: function(){
    console.log(this.name)
  }
}
var obj = {
  name: 'hello'
}

App.sayName()  // app
App.sayName.bind(window)()   // global
App.sayName.bind(obj)()  // hello

一個(gè)函數(shù)名.bind(參數(shù))()相當(dāng)于去生成一個(gè)新的函數(shù),這個(gè)函數(shù)里面的this就是,你傳遞的這個(gè)參數(shù)

這個(gè)新的函數(shù),跟App.sayName(),已經(jīng)不是一個(gè)東西了

作用:

例 1

var Page = {
  init: function(){
    this.node = document.body
    this.bind()
  },
  bind: function(){
    var _this = this
    this.node.addEventListener('click', function(){
      _this.sayHello()
    })
  },
  sayHello: function(){
    console.log('hello...' + this.node.innerText)
  }
}
Page.init()
var Page = {
  init: function(){
    this.node = document.body
    this.bind()
  },
  bind: function(){
    var _this = this
    this.node.addEventListener('click',  this.sayHello)  // this 為 Page 的 this
  },
  sayHello: function(){
    console.log('hello...' + this.node.innerText)
  }  // this 代表當(dāng)前的 dom 節(jié)點(diǎn)
}
Page.init()  // 所以會(huì)報(bào)錯(cuò)
var Page = {
  init: function(){
    this.node = document.body
    this.bind()
  },
  bind: function(){
    var _this = this
    this.node.addEventListener('click',  this.sayHello.bind(this))
    // 第一二個(gè) this 為 Page 的 this,第三個(gè) this 指當(dāng)前節(jié)點(diǎn)
  },
  sayHello: function(){
    console.log('hello...' + this.node.innerText)
  }  // this 代表當(dāng)前的 節(jié)點(diǎn)
}
Page.init()  // 這里實(shí)現(xiàn)跟第一個(gè)例子相同的結(jié)果

例 2

document.addEventListener('click', function(e){
    console.log(this);  // document
    var _document = this;
    setTimeout(function(){
        console.log(this);  // window
        console.log(_document);  // document
    }, 200);
}, false);

如何讓它們?nèi)慷际?code>document,看下面

document.addEventListener('click', function(e){
    console.log(this);  // document
    var _this = this;  // 所以用 .bind(this),就可以不用這么定義了
    setTimeout((function(){
        console.log(this);  // document
        console.log(_this);  // document
    }).bind(this), 200);  // .bind(this)
}, false);

所以用.bind(this),就可以不用定義var _this = this

call | apply 設(shè)置 this


apply相當(dāng)于call的寫法,用數(shù)組傳遞

例 1:遍歷傳入的數(shù)組,做加法

function sum(){
  var result = 0
  for(var i = 0; i < arguments.length; i++){
    console.log(arguments[i])
    result += arguments[i]
  }
  console.log(result)
}
sum(1,2,3)
function sum(){
  var result = 0
  Array.prototype.forEach.call(arguments, function(value){
    console.log(value)
    result += value
  })
  console.log(result)
}
sum(1,2,3)  // 同上面的結(jié)果
function sum(){
  var args = Array.prototype.slice.call(arguments, 0)
  // 通過這句話把 argument 類數(shù)組對(duì)象,變成了一個(gè)數(shù)組
  console.log(Array.isArray(args))
  console.log(args)
}
sum(1,2,3)

例 2:得到數(shù)組的最大值,最小值

Math.max(3,4,5,6)  // 6,Math.max()是一個(gè)方法

var arr = [2,5,0,2,9,6]
Math.max.apply(null, arr)  // 9
Math.min.apply(null, arr)  // 0

三種變量


實(shí)例變量:(this)類的實(shí)例才能訪問到的變量

靜態(tài)變量:(屬性)直接類型對(duì)象能訪問到的變量

私有變量:(局部變量)當(dāng)前作用域內(nèi)有效的變量

例子:

function ClassA(){
    var a = 1; //私有變量,只有函數(shù)內(nèi)部可以訪問
    this.b = 2; //實(shí)例變量,只有實(shí)例可以訪問
}

ClassA.c = 3; // 靜態(tài)變量,也就是屬性,類型訪問

console.log(a); // error
console.log(ClassA.b) // undefined
console.log(ClassA.c) //3

var classa = new ClassA();
console.log(classa.a);//undefined
console.log(classa.b);// 2
console.log(classa.c);//undefined

參考


this 課件
this 的值到底是什么?一次說清楚
你怎么還沒搞懂 this
js 的 new 到底是干什么的

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第3章 基本概念 3.1 語法 3.2 關(guān)鍵字和保留字 3.3 變量 3.4 數(shù)據(jù)類型 5種簡(jiǎn)單數(shù)據(jù)類型:Unde...
    RickCole閱讀 5,527評(píng)論 0 21
  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對(duì)象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學(xué)一百閱讀 3,679評(píng)論 0 4
  • 1.概念 在JavaScript中,this 是指當(dāng)前函數(shù)中正在執(zhí)行的上下文環(huán)境,因?yàn)檫@門語言擁有四種不同的函數(shù)調(diào)...
    BluesCurry閱讀 1,236評(píng)論 0 2
  • 函數(shù)和對(duì)象 1、函數(shù) 1.1 函數(shù)概述 函數(shù)對(duì)于任何一門語言來說都是核心的概念。通過函數(shù)可以封裝任意多條語句,而且...
    道無虛閱讀 4,950評(píng)論 0 5
  • 在深入了解JavaScript中的this關(guān)鍵字之前,先了解一下this關(guān)鍵字的重要性。this 允許復(fù)用函數(shù)時(shí)使...
    小泡_08f5閱讀 389評(píng)論 0 0

友情鏈接更多精彩內(nèi)容