在JavaScript中分配内存 [英] Allocate memory in JavaScript

查看:83
本文介绍了在JavaScript中分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在深入研究JavaScript时遇到了麻烦,请帮助我们。提前致谢

We've encountered a trouble when digging into JavaScript,Please help us. Thanks in advance

以下代码,为什么武士仍然无法调用函数叫喊

Below codes, why samurai still can't call function yell

===>
var ninja = { 
  yell: function yell(n){ 
    return n > 0 ? yell(n-1) + "a" : "hiy"; 
  } 
}; 
assert( ninja.yell(4) == "hiyaaaa", "Works as we would expect it to!" ); 

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

但在这些代码中它无法调用大喊

But in these code it can't call yell

===>
var ninja = { 
  yell: function(n){ 
    return n > 0 ? ninja.yell(n-1) + "a" : "hiy"; 
  } 
}; 
assert( ninja.yell(4) == "hiyaaaa", "A single object isn't too bad, either." ); 

var samurai = { yell: ninja.yell }; 
var ninja = null; 

try { 
  samurai.yell(4); 
} catch(e){ 
  assert( false, "Uh, this isn't good! Where'd ninja.yell go?" ); 
}


推荐答案

在第一个例子中,叫喊名为的函数。在函数中,符号 yell 解析为函数,因此它可以调用自身。因此,你已经消灭了 ninja 对象并不重要。

In the first example, yell is a named function. Within the function, the symbol yell resolves to the function, and so it's able to call itself. So it doesn't matter that you've wiped out the ninja object.

var ninja = { 
  yell: function yell(n){ 
  //             ^^^^-------------------------- the name
    return n > 0 ? yell(n-1) + "a" : "hiy"; 
  //               ^^^^------------------------ using the name
  } 
};

在第二个例子中,叫喊是一个匿名函数,它尝试通过 ninja.yell 调用自身,当 ninja 已被消灭。

In the second example, yell is an anonymous function and it tries to call itself via ninja.yell, which obviously fails when ninja has been wiped out.

var ninja = { 
  yell: function(n){ 
  //            ^-------------------------------------- no name
    return n > 0 ? ninja.yell(n-1) + "a" : "hiy"; 
  //               ^^^^^^^^^^-------------------------- relies on `ninja` object
  } 
}; 






旁注:在你的第一个例子中,你擦拭通过为它分配一个不同的空白对象( ninja = {} )来输出 ninja 对象,但是在第二个例如,您可以通过指定 null ninja = null )来完成此操作。没关系,使用空白对象或 null 在两个示例中都会有相同的结果(尽管第二个示例中收到的错误会改变)。


Side note: In your first example, you wipe out the ninja object by assigning a different, blank object to it (ninja = {}), but in the second example you do it by assigning null (ninja = null). It doesn't matter, using a blank object or null would have the same result in both examples (although the error you receive in the second example would change).

附注2:注意第二个 var ninja = ... 每个示例中的行实际上都被视为 ninja = ... 。构造 var x = y; 实际上是在不同时间发生的两个完全不相关的事情:变量声明, var x ,在进入包含它的执行上下文(松散地,范围)时发生;和赋值操作, x = y; ,这是在逐步执行时到达该行代码时发生的。在范围内有多个声明是无操作。更多: 可怜的误解 var

Side note #2: Note that the second var ninja = ... line in each example is actually treated just as ninja = .... The construct var x = y; is actually two completely unrelated things that happen at different times: The variable declaration, var x, which happens upon entry to the execution context (loosely, "scope") containing it; and an assignment operation, x = y;, which happens when that line of code is reached in the step-by-step execution. Having multiple declarations within a scope is a no-op. More: Poor misunderstood var

这篇关于在JavaScript中分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆