| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Operators/this | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..
this , . this . ES5 bind(), this , . ES2015 , this ( this , ).
const test = {
prop: 42,
func: function () {
return this.prop;
},
};
console.log(test.func());
// Expected output: 42
this
(global, function eval), , .
( - ) this ( ).
// , 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, , .
this , .
, this , global, .window
function f1() {
return this;
}
// :
f1() === window; // window -
// Node:
f1() === global; // global - Node
, this , undefined, :
function f2() {
"use strict"; // . strict mode
return this;
}
f2() === undefined; // true
:
this , undefinedf2 , (, window.f2()). , . window.
// 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'
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'),
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, , .
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 , .
const obj = {
getThisGetter() {
const getter = () => this;
return getter;
},
};
getThisGetter obj, this obj . fn. fn this - getThisGetter, obj. , this globalThis, getThisGetter .
const fn = obj.getThisGetter();
console.log(fn() === obj); // true
obj , getThisGetter , this. fn2()() globalThis, this fn2(), globalThis, - .
const fn2 = obj.getThisGetter;
console.log(fn2()() === globalThis); // true
. this, this . this, this , (, ). setTimeout().
, this , .
, o.f() , this o.
var o = {
prop: 37,
f: function () {
return this.prop;
},
};
console.log(o.f()); // logs 37
, this , . f o. , , o.f. this :
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, ; .
o.b = { g: independent, prop: 42 };
console.log(o.b.g()); // logs 42
this object's prototype , - object's prototype. , this , , .. , , .
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 , /.
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).
/*
* :
*
* 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.
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'), ..
function bar() {
console.log(Object.prototype.toString.call(this));
}
bar.call(7); // [object Number]
, this ( , , addEventListener).
// ,
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-, :
<button > this</button>
'button'. , this DOM- ( ) :
<button >
this
</button>
this , global/window .
| Specification |
|---|
| ECMAScript 2027 LanguageSpecification # sec-this-keyword |
this This page was last modified on 17 . 2025 . by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |