Bu eğitimde, örnekler yardımıyla JavaScript'in bu anahtar kelimesini öğreneceksiniz.
JavaScript'te this
anahtar kelime, çağrıldığı nesneyi ifade eder.
1. Küresel Kapsam İçinde
Zaman this
, tek başına kullanıldığında, this
genel nesneyi (belirtmektedir window
tarayıcılarda nesne). Örneğin,
let a = this; console.log(a); // Window () this.name = 'Sarah'; console.log(window.name); // Sarah
Burada this.name
da aynısı window.name
.
2. bu İç İşlevi
Bir this
işlevde kullanıldığında this
, genel nesneyi ifade eder ( window
tarayıcılardaki nesne). Örneğin,
function greet() ( // this inside function // this refers to the global object console.log(this); ) greet(); // Window ()
3. Bu İç Yapıcı İşlevi
JavaScript'te, nesneler oluşturmak için yapıcı işlevler kullanılır. Bir işlev bir yapıcı işlev olarak kullanıldığında, this
içinde kullanıldığı nesneyi ifade eder. Örneğin,
function Person() ( this.name = 'Jack'; console.log(this); ) let person1 = new Person(); console.log(person1.name);
Çıktı
Kişi (adı: "Jack") Jack
Burada this
person1 nesnesini ifade eder. Bu yüzden person1.name
bize Jack verir.
Not : this
ES6 sınıflarıyla birlikte kullanıldığında, içinde kullanıldığı nesneyi ifade eder (yapıcı işlevlerine benzer).
4. bu İç Nesne Yöntemi
Bir this
nesnenin yönteminin içinde kullanıldığında, içinde this
bulunduğu nesneyi ifade eder. Örneğin,
const person = ( name : 'Jack', age: 25, // this inside method // this refers to the object itself greet() ( console.log(this); console.log(this.name); ) ) person.greet();
Çıktı
(isim: "Jack", yaş: 25, selam: ƒ) Jack
Yukarıdaki örnekte, nesneyi this
ifade eder person
.
5. bu İç İç Fonksiyon
Bir this
iç işleve eriştiğinizde (bir yöntemin içinde), this
global nesneyi ifade eder. Örneğin,
const person = ( name : 'Jack', age: 25, // this inside method // this refers to the object itself greet() ( console.log(this); // (name: "Jack", age… ) console.log(this.age); // 25 // inner function function innerFunc() ( // this refers to the global object console.log(this); // Window (… ) console.log(this.age); // undefined ) innerFunc(); ) ) person.greet();
Çıktı
(isim: "Jack", yaş: 25, selamlama: ƒ) 25 Pencere (…) tanımsız
Burada, this
iç innerFunc()
belirtir küresel nesne için innerFunc()
bir usul içinde.
Bununla birlikte, this.age
dışarısı nesneyi innerFunc()
ifade eder person
.
6. bu İç Ok İşlevi
Ok işlevinin içinde, this
ana kapsamı ifade eder. Örneğin,
const greet = () => ( console.log(this); ) greet(); // Window (… )
Ok işlevlerinin kendilerine ait özellikleri yoktur this
. Bir this
ok işlevinin içinde kullandığınızda this
, üst kapsam nesnesine başvurur. Örneğin,
const greet = ( name: 'Jack', // method sayHi () ( let hi = () => console.log(this.name); hi(); ) ) greet.sayHi(); // Jack
Burada işlevin this.name
içinde nesneye hi()
atıfta bulunulmaktadır greet
.
undefined
Bir yöntem içinde bir işlevi kullanırken sahip olma sorununu çözmek için ok işlevini de kullanabilirsiniz (Örnek 5'te görüldüğü gibi). Örneğin,
const person = ( name : 'Jack', age: 25, // this inside method // this refers to the object itself greet() ( console.log(this); console.log(this.age); // inner function let innerFunc = () => ( // this refers to the global object console.log(this); console.log(this.age); ) innerFunc(); ) ) person.greet();
Çıktı
(isim: "Jack", yaş: 25, selamlama: ƒ) 25 (adı: "Jack", yaş: 25, selam: ƒ) 25
Burada, innerFunc()
ok işlevi kullanılarak tanımlanır. this
Üst kapsamından alır . Dolayısıyla 25this.age
verir .
Ok işlevi ile birlikte kullanıldığında this
, dış kapsamı ifade eder.
7. Sıkı Modlu Bu İç İşlev
Ne zaman this
yüksek düzey moduyla bir fonksiyonu kullanılır, this
olduğu undefined
. Örneğin,
'use strict'; this.name = 'Jack'; function greet() ( // this refers to undefined console.log(this); ) greet(); // undefined
Not : this
Katı modda bir işlevin içinde kullanırken, JavaScript Function call () kullanabilirsiniz.
Örneğin,
'use strict'; this.name = 'Jack'; function greet() ( console.log(this.name); ) greet.call(this); // Jack
Geçmek zaman this
ile call()
fonksiyonu greet()
yöntemiyle olarak kabul edilir this
bir nesne (bu durumda global nesne).