JavaScript: "bu" nedir?

Dec 13 2022
Merhaba , Bu yazımızda this anahtar kelimesinin JavaScript'te kullanıldığı bağlama bağlı olarak olası farklı değerlerini açıklamaya çalışacağız. Global kapsam Global kapsamda bu, global nesneyi (web üzerindeki pencere ve Node.js'deki global) ifade eder.

Merhaba ,

thisBu yazıda, JavaScript'te kullanıldığı bağlama göre anahtar kelimenin olası farklı değerlerini açıklamaya çalışacağız .

küresel kapsam

Global kapsamda, thisglobal nesneyi ifade eder ( windowweb'de ve globalNode.js'de).

globalThisküresel nesneye ulaşmanın platformdan bağımsız bir yolu olarak da kullanılabilir.

// global scope (web)

console.log(this === window); // true
console.log(this === globalThis); // true

Ok olmayan bir işlevin içinde değeri, işlevin nasıl çağrıldığınathis bağlı olacaktır .

Bağımsız bir işlev olarak çağrılır

  • Katı olmayan modda , global nesneyi ifade eder: this
  • function getThis() {
      return this;
    }
    
    console.log(getThis() === globalThis); // true
    

    "use strict"
    
    function getThis() {
      return this;
    }
    
    console.log(getThis() === undefined); // true
    

const bar = {
  getThis: function() {
    return this;
  }
};

console.log(bar.getThis() === bar); // true

const baz = {};
baz.getThis = bar.getThis;

console.log(baz.getThis() === baz); // true

// Called as a standalone function
const getThis = bar.getThis;
console.log(getThis() === globalThis); // true (`false` in strict mode)

Sınıf yöntemleri, nesne hazır bilgilerindeki yöntemlerle aynı şekilde davranır.

Ok işlevleri

Bir ok işlevinin içindeki değeri this , çevreleyen sözcüksel bağlamın birine eşittir .

Başka bir deyişle, bir ok işlevini değerlendirirken, JS motoru yeni bir thisbağlama oluşturmaz, ancak onu çevreleyen kapsamda mevcut olanı kullanır :

// (1) `this` equals to `globalThis` here

const foo = {
  getThis: () => this
};

console.log(foo.getThis() === foo); // false
console.log(foo.getThis() === globalThis); // true (1)
// (1) `this` equals to `globalThis` here

const bar = {
  getArrowFn: function() {
    // (2) `this` equals to `bar` or `baz` here 
    // depending on how the function is called below
    
    return () => this;
  }
};

console.log(bar.getArrowFn()() === bar); // true (2)
console.log(bar.getArrowFn()() === globalThis); // false (1)

const baz = {};
baz.getArrowFn = bar.getArrowFn;

console.log(baz.getArrowFn()() === baz); // true (2)
console.log(baz.getArrowFn()() === globalThis); // false (1)

işlev kurucuları

Yapıcı olarak kullanılan bir fonksiyonun içinde ( newanahtar kelime ile), thisfonksiyona nasıl erişilirse erişilsin inşa edilmekte olan yeni nesneye bağlıdır:

function Bar() {
  this.baz = 42;
}

let a = new Bar();
console.log(a.baz); // 42
const bar = {
  baz: function() {
    this.foo = 42;
  }
};

let a = new bar.baz();
console.log(a.foo); // 42
console.log(bar.foo); // undefined

function Bar() {
  this.baz = 42;
  return { baz: 44 };
}

let a = new Bar();
console.log(a.baz); // 44 (not 42)

uygula(), çağrı()

ve yöntemleri call(), ok olmayan işlevlerdeapply() değerini açıkça ayarlamak için kullanılabilir :this

function getThis() {
  return this;
}

const bar = {
  getThis: function() {
    return this;
  }
};

console.log(getThis() === globalThis); // true
console.log(bar.getThis() === bar); // true

const myThis = {};

console.log(getThis.call(myThis) === myThis); // true
console.log(getThis.apply(myThis) === myThis); // true

console.log(bar.getThis.call(myThis) === myThis); // true
console.log(bar.getThis.apply(myThis) === myThis); // true

Bir ok işlevinde kullanıldığında, thisparametre yoksayılır (yine de bu yöntemleri kullanarak işlev bağımsız değişkenlerini iletebilirsiniz):

// (1) `this` equals to `globalThis` here

const getThis = () => this;

console.log(getThis() === globalThis); // true

const myThis = {};

console.log(getThis.call(myThis) === globalThis); // true
console.log(getThis.apply(myThis) === globalThis); // true

İşlev bind(), belirli bir nesneye bağlı yeni bir işlev oluşturmak için kullanılır.

Bu, yeni işlev çağrıldığında, thisanahtar kelimenin bağlı olduğu nesneye atıfta bulunacağı anlamına gelir:

function getThis() {
  return this;
}

console.log(getThis() === globalThis); // true

const myThis = {};
const getMyThis = getThis.bind(myThis);

console.log(getMyThis() === myThis); // true
console.log(getMyThis.call(globalThis) === myThis); // true
console.log(getMyThis.apply(globalThis) === myThis); // true

Bir ok işlevinde kullanıldığında, thisparametre yoksayılır (yine de bu yöntemleri kullanarak işlev bağımsız değişkenlerini iletebilirsiniz):

// (1) `this` equals to `globalThis` here

const getThis = () => this;

console.log(getThis() === globalThis); // true

const myThis = {};
const getMyThis = getThis.bind(myThis);

console.log(getMyThis() === globalThis); // true
console.log(getMyThis.call(myThis) === globalThis); // true
console.log(getMyThis.apply(myThis) === globalThis); // true