Frage

var ninja = {
  yell: function yellaaa(n){
    return n > 0 ? yell(n-1) + "a" : "hiy";
  }
};

var samurai = { yell: ninja.yell };
var ninja = null;
assert( samurai.yell(4) == "hiyaaaa", "The method correctly calls itself." );

I would like to ask, why samurai.yell can still be called after ninja.null is being deleted? Does this mean by giving object method a name, the copy becomes a "deep copy", while anonymous function will only conduct a "shallow copy"?

Thanks

War es hilfreich?

Lösung

Functions are objects as well. While Ninja creates yell function object, later in code you reference it by Samurai. So now you have 2 references to the same (function) object. Later you "delete" Ninja (which is one of those 2 referees) but you still hold reference in Samurai.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top