2
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." );

我想问一下,为什么 samurai.yell 在 ninja.null 被删除后仍然可以调用?这是否意味着通过给对象方法一个名称,副本成为“深拷贝”,而匿名函数只会进行“浅拷贝”?

谢谢

4

1 回答 1

1

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.

于 2013-06-10T08:38:48.347 回答