Ad

Can't Understand This Code

- 1 answer

I can't understand this code. please help me

function HelloFunc(func){
    this.greeting = "hello";
}

HelloFunc.prototype.call = function(func){
    func? func(this.greeting) : this.func(this.greeting);
}

var userFunc = function(greeting){
     console.log(greeting);
}

var objHello = new HelloFunc();
objHello.func = userFunc;
objHello.call();

func? func(this.greeting) : this.fun(this.greeting);

what's that mean?

also, I can't understand that code on the whole please explane that code

Ad

Answer

It's the ternary ?:-operator and can be rewritten as:

if (func) {
    func(this.greeting);
} else {
    this.func(this.greeting);
}

Only difference is, that the original expression has a value, but in the code snippet above it isn't used anyway.

Ad
source: stackoverflow.com
Ad