關于 JS this
- 1. this 與 普通函式
- 2. this 與 bind、call、apply
- 3. this 與 箭頭函式
- 4. this 與 return
- 4.1 回傳 參考物件
- 4.2 回傳 function物件
- 4.3 回傳 數字,值物件
- 4.4 回傳 undefined
- 4.5 回傳 null
1. this 與 普通函式
普通函式 this 永遠指向它的呼叫物件
var obj = {
a:10,
b:{
a:20,
fn:function(){
console.log(this.a);
}
}
}
obj.b.fn(); //=> 20
物件 b 呼叫 fn 函式 this.a 為 20,
var obj2 = {
a:10,
b:{
fn:function(){
console.log(this.a);
}
}
}
obj2.b.fn(); //=> undefined
物件 b 中無 a 欄位, this.a 為 undefined,
var obj3 = {
a:10,
b:{
a:20,
fn:function(){
console.log(this);
console.log(this.a);
}
}
}
var j = obj3.b.fn;
j(); //=> window, undefined
這里直接獲取里fn物件,沒有通過物件去呼叫,非嚴格模式下,this 默認指向全域物件window,
var obj4 = {
a:10,
b:{
a:20,
fn:function(){
"use strict";
console.log(this);
console.log(this.a);
}
}
}
var g = obj4.b.fn;
g(); //=> undefined, TypeError: Cannot read property 'a' of undefined
而嚴格模式下, this 為 undefined,
2. this 與 bind、call、apply
function.bind(thisArg[, arg1[, arg2[, ...]]]) 最簡單的用法是創建一個函式,不論怎么呼叫,這個函式都有同樣的 this 值,
function.call(thisArg, arg1, arg2, ...)、function.apply(thisArg, [argsArray]) call、apply 的作用類似都是為呼叫函式指定this,
function fn() {
return this.user;
}
console.log(fn.call({ user: "li" })); //=> li
console.log(fn.apply({ user: "wang" })); //=> wang
var bfn = fn.bind({ user: "gao" });
console.log(bfn()); //=> gao
console.log(bfn.call({ user: "liu" })); //=> gao
console.log(bfn.apply({ user: "liu" })); //=> gao
console.log(bfn.bind({ user: "liu" })()); //=> gao
var obj = { user: 'zhang', f: fn, g: bfn };
console.log(obj.f(), obj.g()); //=> zhang, gao
對 bind 產生的函式使用,再使用 call、apply、bind, 指向的 this 仍然是初次bind的值,
function list() {
return Array.prototype.slice.call(arguments);
}
console.log(list(1, 2, 3)); //=> [1, 2, 3]
function list2(){
if(arguments){
arguments.__proto__.slice = Array.prototype.slice;
return arguments.slice();
}
}
console.log(list2(4, 5, 6)); //=> [4, 5, 6]
var leading37List = list.bind(null, 37);
console.log(leading37List()); //=> [37]
console.log(leading37List(1, 2, 3)); //=> [37, 1, 2, 3]
在函式代碼中,特殊物件 arguments 無需明確指出引數名,就能訪問它們,arguments 自帶 length 屬性, Array.prototype.slice.call(arguments) 可以理解成 arguments.slice()
3. this 與 箭頭函式
由于箭頭函式不系結this,它會捕獲其所在(即定義的位置)背景關系的this值, 作為自己的this值,
var obj = {
a: 10,
b: function () {
console.log(this.a, this)
},
c: () => console.log(this.a, this),
}
obj.b(); //=> 10, Object{...}
obj.c(); //=> undefined, window{...}
obj.c.apply({ a: 'apply' }); //=> undefined, window{...}
obj.c.call({ a: 'call' }); //=> undefined, window{...}
obj.c.bind({ a: 'bind' })(); //=> undefined, window{...}
call() 、 apply() 、 bind() 方法對于箭頭函式來說只是傳入引數,對它的 this 毫無影響,
function fn() {
this.a = 20;
this.b = () => console.log(this.a, this);
}
console.log((new fn()).b()) //=> 20, fn {a: 20, b: ?}
箭頭函式不會創建自己的this,它只會從自己的作用域鏈的上一層繼承this,
var lam = (...r) => {
console.log(r);
}
lam(1,2,3); //=> Array : [1, 2, 3]
箭頭函式沒有arguments,取而代之用rest引數…解決,
var B = ()=>{
value:1;
}
var b = new B(); //=> TypeError: B is not a constructor
箭頭函式作為匿名函式,沒有建構式,不能使用new,
4. this 與 return
如果回傳值是 參考型別 物件,那么this指向的就是那個回傳的物件;
如果回傳值是 非參考型別 物件那么this還是指向函式的實體,
4.1 回傳參考物件
function fn()
{
this.user = '123';
return {};
}
var a = new fn;
console.log(a.user); //=> undefined
4.2 回傳function物件
function fn()
{
this.user = '123';
return function(){};
}
var a = new fn;
console.log(a.user); //=> undefined
4.3 回傳數字,值物件
function fn()
{
this.user = '123';
return 123;
}
var a = new fn;
console.log(a.user); //=> 123
4.4 回傳undefined
function fn()
{
this.user = '123';
return undefined;
}
var a = new fn;
console.log(a.user); //=> 123
4.5 回傳null
function fn()
{
this.user = '123';
return null;
}
var a = new fn;
console.log(a.user); //=> 123
回傳目錄
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/109850.html
標籤:Html/Css
