2014-04-10 5 views
-5

안녕하세요 친구는 정말로 this javascript 속성에 붙어 있습니다.이것은 javascript에서 혼란 스럽습니다

Collection.prototype.onOpen = function() { 

    var self = this; 

    this.buffer = function() { 
return this 
} 

    self.doQueue(); 
}; 

난 그냥 here..Is this 여기 컬렉션에 해당하는 것을 this는 해당 알아야합니다.

..Any 도움이 ..Thanks

+1

를 변경하는 방법에 대한 이해에 도움이됩니다, this' 일 무슨'말을하는 것은 불가능합니다. 그것은 완전히 "onOpen"함수가 어떻게 호출되는지에 달려 있습니다. – Pointy

+0

범위 지정을 읽어야하지만, 확실하지 않은 경우 console.log에없는 이유는 무엇입니까? 그것은 jiffy에서 알려줄 것입니다. –

+0

'this'에 대한 MDN 문서를 읽는 것이 좋습니다 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this. –

답변

2

함수가 호출 된 방법에 따라 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` 
+0

여기에 컬렉션이 여기에 해당됩니다. 그렇습니까? – user3517846

+0

@ user3517846 : 아니오 –

+3

@ user3517846 당신이 그것을 어떻게 불러야하는지 우리에게 알려주지 않았습니다. –