Rx.Observer.prototype.asObserver()

Hides the identity of an observer.

返回值

(Observer): An observer that hides the identity of the specified observer.

Example

function SampleObserver () {
    Rx.Observer.call(this);
    this.isStopped = false;
}

SampleObserver.prototype = Object.create(Rx.Observer.prototype);
SampleObserver.prototype.constructor = SampleObserver;

Object.defineProperties(SampleObserver.prototype, {
    onNext: {
        value: x => {
            if (!this.isStopped) {
                console.log(`Next: ${x}`);
            }
        }
    },
    onError: {
        value: err => {
            if (!this.isStopped) {
                this.isStopped = true;
                console.log(`Error: ${err}`);
            }
        }
    },
    onCompleted: {
        value: () => {
            if (!this.isStopped) {
                this.isStopped = true;
                console.log('Completed');
            }
        }
    } 
});

var sampleObserver = new SampleObserver();

var source = sampleObserver.asObserver();

console.log(source === sampleObserver);
// => false