0

Object.observe 기능을 사용하기 위해 알림 (Thingy 사용) 아래 여기에 게시 된 예제를 실행하려고합니다.이 Object.observe 알림 예제가 작동하지 않는 이유

function Thingy(a, b, c) { 
    this.a = a; 
    this.b = b; 
} 

Thingy.MULTIPLY = 'multiply'; 
Thingy.INCREMENT = 'increment'; 
Thingy.INCREMENT_AND_MULTIPLY = 'incrementAndMultiply'; 


Thingy.prototype = { 
    increment: function(amount) { 
    var notifier = Object.getNotifier(this); 

    notifier.performChange(Thingy.INCREMENT, function() { 
     this.a += amount; 
     this.b += amount; 
    }, this); 

    notifier.notify({ 
     object: this, 
     type: Thingy.INCREMENT, 
     incremented: amount 
    }); 
    }, 

    multiply: function(amount) { 
    var notifier = Object.getNotifier(this); 

    notifier.performChange(Thingy.MULTIPLY, function() { 
     this.a *= amount; 
     this.b *= amount; 
    }, this); 

    notifier.notify({ 
     object: this, 
     type: Thingy.MULTIPLY, 
     multiplied: amount 
    }); 
    }, 

    incrementAndMultiply: function(incAmount, multAmount) { 
    var notifier = Object.getNotifier(this); 

    notifier.performChange(Thingy.INCREMENT_AND_MULTIPLY, function() { 
     this.increment(incAmount); 
     this.multiply(multAmount); 
    }, this); 

    notifier.notify({ 
     object: this, 
     type: Thingy.INCREMENT_AND_MULTIPLY, 
     incremented: incAmount, 
     multiplied: multAmount 
    }); 
    } 
} 

var observer = { 
    records: undefined, 
    callbackCount: 0, 
    reset: function() { 
     this.records = undefined; 
     this.callbackCount = 0; 
    }, 
} 
var observer2 = { 
    records: undefined, 
    callbackCount: 0, 
    reset: function() { 
     this.records = undefined; 
     this.callbackCount = 0; 
    }, 
}; 

observer.callback = function(r) { 
    console.log(r); 
    observer.records = r; 
    observer.callbackCount++; 
}; 

observer2.callback = function(r){ 
    console.log('Observer 2', r); 
} 

Thingy.observe = function(thingy, callback) { 
    // Object.observe(obj, callback, optAcceptList) 
    Object.observe(thingy, callback, [Thingy.INCREMENT, 
            Thingy.MULTIPLY, 
            Thingy.INCREMENT_AND_MULTIPLY, 
            'update']); 
} 

Thingy.unobserve = function(thingy, callback) { 
    Object.unobserve(thingy); 
} 

var thingy = new Thingy(2, 4); 

// Observe thingy 
Object.observe(thingy, observer.callback); 
Thingy.observe(thingy, observer2.callback); 

// Play with the methods thingy exposes 
thingy.increment(3);    // { a: 5, b: 7 } 
thingy.b++;      // { a: 5, b: 8 } 
thingy.multiply(2);    // { a: 10, b: 16 } 
thingy.a++;      // { a: 11, b: 16 } 
thingy.incrementAndMultiply(2, 2); // { a: 26, b: 36 } 

내가 TypeError: undefined is not a function을 받기를 실행하려고 : 여기가 실행 된 코드입니다. 나는 Object.observe에 익숙하지 않아서, 왜 에러가 발생하고 어떻게 고칠 수 있습니까?

참고 : Chrome 33 이상에서만 제공되는 Object.observe이 필요합니다.

답변

0

의 오해로 인해 오류가 발생합니다.입니다. 코드에서, 당신은 : performChange 누구 알리미 촬영 개체 자체를 의미하지

var notifier = Object.getNotifier(this); 

notifier.performChange(Thingy.MULTIPLY, function() { 
    this.a *= amount; 
    this.b *= amount; 
}, this); 

내부. 이를 사용하려면 다음을 수정하십시오.

var notifier = Object.getNotifier(this); 
var self = this; 

notifier.performChange(Thingy.MULTIPLY, function() { 
    self.a *= amount; 
    self.b *= amount; 
}, this); 

모든 알리미를 변경하면 코드가 예상대로 작동합니다.

관련 문제