[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/this [Back]  [Original]

this - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

this

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

this JavaScript . this .

this , . this . ES5 bind(), this , . ES2015 , this ( this , ).

In this article

const test = {
  prop: 42,
  func: function () {
    return this.prop;
  },
};

console.log(test.func());
// Expected output: 42

this

(global, function eval), , .

Global

( - ) this ( ).

js
//  ,  window    global:
console.log(this === window); // true

a = 37;
console.log(window.a); // 37

this.b = "MDN";
console.log(window.b); // "MDN"
console.log(b); // "MDN"

: global, globalThis, , .

Function

this , .

, this , global, window.

js
function f1() {
  return this;
}

//  :
f1() === window; // window -    

//  Node:
f1() === global; // global -    Node

, this , undefined, :

js
function f2() {
  "use strict"; // . strict mode
  return this;
}

f2() === undefined; // true

: this undefined, f2 , (, window.f2()). , . window.

, this , call() apply(), .

1

js
//      call  apply    ,
//     this.
var obj = { a: "Custom" };

//     
var a = "Global";

function whatsThis() {
  return this.a; // this     
}

whatsThis(); // 'Global'
whatsThis.call(obj); // 'Custom'
whatsThis.apply(obj); // 'Custom'

2

js
function add(c, d) {
  return this.a + this.b + c + d;
}

var o = { a: 1, b: 3 };

//   -      
// 'this',    
//   call
add.call(o, 5, 7); // 16

//   -      
// 'this',  - ,   
//     call
add.apply(o, [10, 20]); // 34

, , , call apply this, , ToObject. , , 7 'foo', Object , 7 , new Number(7), 'foo' - new String('foo'),

js
function bar() {
  console.log(Object.prototype.toString.call(this));
}

bar.call(7); // [object Number]
bar.call("foo"); // [object String]

bind()

f.bind(someObject) , f, this bind, , .

js
function f() {
  return this.a;
}

const g = f.bind({ a: "qwerty" });
console.log(g()); // qwerty

const h = g.bind({ a: "yoo" }); // bind    !
console.log(h()); // qwerty

const o = { a: 37, f, g, h };
console.log(o.a, o.f(), o.g(), o.h()); // 37 37 qwerty qwerty

this

this . obj getThisGetter, , this. , this this . this getThisGetter , , , . , getThisGetter , .

js
const obj = {
  getThisGetter() {
    const getter = () => this;
    return getter;
  },
};

getThisGetter obj, this obj . fn. fn this - getThisGetter, obj. , this globalThis, getThisGetter .

js
const fn = obj.getThisGetter();
console.log(fn() === obj); // true

obj , getThisGetter , this. fn2()() globalThis, this fn2(), globalThis, - .

js
const fn2 = obj.getThisGetter;
console.log(fn2()() === globalThis); // true   

. this, this . this, this , (, ). setTimeout().

, this , .

, o.f() , this o.

js
var o = {
  prop: 37,
  f: function () {
    return this.prop;
  },
};

console.log(o.f()); // logs 37

, this , . f o. , , o.f. this :

js
var o = { prop: 37 };

function independent() {
  return this.prop;
}

o.f = independent;

console.log(o.f()); // logs 37

, , f o.

, this . , , g o.b. , this, , o.b. , o, ; .

js
o.b = { g: independent, prop: 42 };
console.log(o.b.g()); // logs 42

this object's prototype

, - object's prototype. , this , , .. , , .

js
var o = {
  f: function () {
    return this.a + this.b;
  },
};
var p = Object.create(o);
p.a = 1;
p.b = 4;

console.log(p.f()); // 5

, p, f, . , , f o. p.f, this f p. , f p, this p. JS.

this /

, . , this , /.

js
function modulus() {
  return Math.sqrt(this.re * this.re + this.im * this.im);
}

var o = {
  re: 1,
  im: -1,
  get phase() {
    return Math.atan2(this.im, this.re);
  },
};

Object.defineProperty(o, "modulus", {
  get: modulus,
  enumerable: true,
  configurable: true,
});

console.log(o.phase, o.modulus); // logs -0.78 1.4142

( new ), this .

: , this, ( , this).

js
/*
 *    :
 *
 * function MyConstructor(){
 *   //  ,   .
 *   //    |this| 
 *   // ,   ; ,
 *   this.fum = "nom";
 *   //  ..
 *
 *   //    ,
 *   //  ,   
 *   //   |new|.   ,
 *   //   - ,
 *   //      |this|
 *   // (..    ).
 * }
 */

function C() {
  this.a = 37;
}

var o = new C();
console.log(o.a); // logs 37

function C2() {
  this.a = 37;
  return { a: 38 };
}

o = new C2();
console.log(o.a); // logs 38

(C2), - , , , this, . ( "this.a = 37;" "" . , , - .)

call apply

this, call apply, Function.prototype.

js
function add(c, d) {
  return this.a + this.b + c + d;
}

var o = { a: 1, b: 3 };

//   -  ,    
// 'this',   
//     
add.call(o, 5, 7); // 1 + 3 + 5 + 7 = 16

//   - ,    
// 'this',   - ,
//        
add.apply(o, [10, 20]); // 1 + 3 + 10 + 20 = 34

, call apply this, , , ToObject. , 7 'foo', , 7 new Number(7), 'foo' new String('foo'), ..

js
function bar() {
  console.log(Object.prototype.toString.call(this));
}

bar.call(7); // [object Number]

DOM

, this ( , , addEventListener).

js
//    ,    
function bluify(e) {
  //  true
  console.log(this === e.currentTarget);
  // true,  currentTarget  target  
  console.log(this === e.target);
  this.style.backgroundColor = "#A5D9F3";
}

//      
var elements = document.getElementsByTagName("*");

//  bluify   ,  
//      
for (var i = 0; i < elements.length; i++) {
  elements[i].addEventListener("click", bluify, false);
}

, this DOM-, :

js
<button > this</button>

'button'. , this DOM- ( ) :

js
<button >
    this
</button>

this , global/window .

Specification
ECMAScript 2027 LanguageSpecification
# sec-this-keyword


Web Proxy Viewer  |  New URL  |  Original Page