var f = function() { this.x = 5; (function() { this.x = 3; })(); console.log(this.x); }; var obj = {x: 4, m: function() { console.log(this.x); }}; f(); new f(); obj.m(); new obj.m(); f.call(f); obj.m.call(f);
function f() { console.log(this === window); // true } f();
(function () { console.log(this === window); // true })();
function f() { this.x = 5; console.log(this === window); // false } var o = new f(); console.log(ox === 5); // true
var o = { f: function() { return this; } } console.log(of() === o); // true
var o = { f: function() { return this; } } var o2 = {f: of}; console.log(of() === o);//true console.log(o2.f() === o2);//true
function f(a,b,c) { return a * b + c; } f.call(f, 1, 2, 3); // ; var args = [1,2,3]; f.apply(f, args); // // ; // <b>f</b> a = 1, b = 2, c = 3;
function f() { } f.call(window); // this f window f.call(f); //this f f
function f() { console.log(this.toString()); // 123 } f.call(123); // this f Number 123
var f = function() { // f - f(), // this this.x = 5; // window.x = 5; // 1.1 , this (function() { this.x = 3; // window.x = 3 })(); console.log(this.x); // console.log(window.x) };
var f = function() { // f new, // this ( object) this.x = 5; // object.x = 5; // 1.1 , this (function() { this.x = 3; // window.x = 3 })(); console.log(this.x); // console.log(object.x) };
var obj = {x: 4, m: function() { // 1.3 , this === obj, console.log(this.x); // console.log(obj.x) } };
var obj = {x: 4, m: function() { // 1.2 , this // this.x, - undefined console.log(this.x); } };
var f = function() { // f call // call ( ) f, // this f this.x = 5; // fx = 5; // 1.1 , this (function() { this.x = 3; // window.x = 3 })(); console.log(this.x); // console.log(fx) };
var obj = {x: 4, m: function() { // call // f // this f // fx 5, 5 console.log(this.x); // console.log(fx) } };
var f = function() { this.x = 5; (function() { this.x = 3; })(); console.log(this.x); }; var obj = {x: 4, m: function() { console.log(this.x); }}; obj.m.call(f);
Source: https://habr.com/ru/post/149516/
All Articles