함수가 호출 된 방법에 따라 this
변화의 값을 주시면 감사하겠습니다 제발 도와주세요;
Collection.prototype.onOpen(); // `this` refers to `Collection.prototype`
// vs
var c = new Collection();
c.onOpen(); // `this` refers to `c`
// vs
var o = {};
Collection.prototype.onOpen.call(o); // `this` refers to `o`
// vs
var foo = Collection.prototype.onOpen;
foo(); // `this` could be `window` or undefined
아마도 몇 가지 예 this
그냥 당신이 게시 그 코드를 찾고
// set up
var o = {}; // So we have an empty Object called `o`
function foo() { // and a function called `foo` which lets
return this; // us see the value of `this` by returning
} // it
o.bar = foo; // and set `bar` of `o` to be `foo`, too
// example 1
foo(); // `window` if in global scope (see example 4 for other scopes)
// example 2
o.bar(); // `o`
// example 3
foo.call(o); // `o`, same as `o.bar.call(o);`
foo.apply(o); // `o`, same as `o.bar.apply(o);`
// example 4, complex
(function() { // IIFE creates a new scope
"use strict"; // and let's have it in in strict mode
function baz() {
return this;
}
return baz();
}()); // = `this` from `baz` = undefined
// example 5, inheritance
function Fizz() {
}
Fizz.prototype.buzz = foo;
var fizz = new Fizz();
fizz.buzz(); // `fizz`
를 변경하는 방법에 대한 이해에 도움이됩니다, this' 일 무슨'말을하는 것은 불가능합니다. 그것은 완전히 "onOpen"함수가 어떻게 호출되는지에 달려 있습니다. – Pointy
범위 지정을 읽어야하지만, 확실하지 않은 경우 console.log에없는 이유는 무엇입니까? 그것은 jiffy에서 알려줄 것입니다. –
'this'에 대한 MDN 문서를 읽는 것이 좋습니다 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this. –