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.
Javascript-scope of object method [duplicate]
-
31-05-2022 - |
문제
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
해결책
제휴하지 않습니다 StackOverflow