문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top