0

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

YankeeCki
  • 41
  • 4

1 Answers1

0

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.

Ctx
  • 17,064
  • 24
  • 33
  • 48
  • how does 'func' work in that code? – YankeeCki Jan 24 '16 at 01:52
  • `func` seems to be an optional parameter, which holds a function (which is a first-order value in javascript) when set. `if (func)` checks, if it was passed by the caller or not. In the first case, this function is called, in the latter case some default function is. – Ctx Jan 24 '16 at 01:54
  • thank you ! you're a hero – YankeeCki Jan 24 '16 at 01:59