Sallu
Sallu

Reputation: 489

What is this line is in Angular syntax?

Unfortunately I do not understand following syntax. I know what it does, it removes a listener for the listener's array.

this._listeners = this._listeners.filter(1 => 1 !== listener);

I do not understand this syntax in Angular2 and it is used everywhere. 1 => 1 !== listener.

The whole code snippet is

subscribe(listener: ListenerCallback): UnsubscribeCallback {
    this._listeners.push(listener);

    return () => { // returns an unsubscribe function
        this._listener = this._listeners.filter(1 => 1 !== listener);
    };
}

Upvotes: 1

Views: 108

Answers (2)

anoop
anoop

Reputation: 3822

1 => 1 !== listener

This is ES6 arrow function, read more here.

For example, var even = [1,2,3,4].filter(v => v % 2 == 0); will give your even numbers. Try this in the console of your browser.

And in your question you just need to replace 1 => 1 !== listener, with item => item !== listener.

Upvotes: 1

eko
eko

Reputation: 40647

I think that's a typo in your book.

Assuming it's

this._listener = this._listeners.filter(listener => 1 !== listener);

This will filter _listeners array and

will return any element which is not equal to 1 ( listener => 1 !== listener )

That's a short hand syntax for

this._listener = this._listeners.filter((listener) =>{ 
    return 1 !== listener
});

which is shorter for

this._listener = this._listeners.filter((listener) =>{ 
    if(1 !== listener){
      return listener;
    }    
});

Upvotes: 2

Related Questions