javascript中的对象null和{}行为 [英] an object null and { } behaviour in javascript

查看:111
本文介绍了javascript中的对象null和{}行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Program1

  var ninja = {
yell:function(n){
return n> 0? ninja.yell(n-1)+a:hiy;
}
};
assert(ninja.yell(4)==hiyaaaa,单个对象也不算太坏,);

var samurai = {yell:ninja.yell}; //ninja.yell已经在ninja = null之前分配了
var ninja = null;

尝试{
samurai.yell(4); ///为什么这个声明不能执行??????????
} catch(e){
assert(false,呃,这不太好,哪里有ninja.yell?);

Program2

  var ninja = {
yell:function yell(n){
return n> 0?大喊(n-1)+a:hiy;
}
};
assert(ninja.yell(4)==hiyaaaa,按照我们预期的那样工作!);

var samurai = {yell:ninja.yell}; ////ninja.yell已经在ninja = {}
var ninja = {}之前分配过了; //
assert(samurai.yell(4)==hiyaaaa,方法正确调用自身。); //为什么这个工作在上面的代码工作的时候


$ b

正如你所看到的这两个例子几乎相同(根据我有限的知识),第二个例子是在第一个例子不。



至此,我的问题是,在Program1 samurai.yell (4)未执行。但是,在Program2 samurai.yell(4)正在执行。两者几乎是一样的,但为什么第二次执行的时候,第一次不执行?解析方案

这与此无关之间的区别 null {} ,它只取决于 ninja.yell <

第一个例子不起作用,因为在 ninja.yell 函数,你再次引用 ninja

  return n> 0? ninja.yell(n-1)+a:hiy; 

因此,如果您稍后分配 null to ninja ,这段代码会抛出一个错误,因为 null 没有一个属性大声






第二个例子的作用是因为那正是不是在做。而不是引用持有该函数的对象( ninja ),您正在给该函数一个名称并直接引用该名称:

 函数yell(n){
return n> 0?大喊(n-1)+a:hiy;
}

该函数基本上是自包含的,它不依赖于自由变量的值。

Program1

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 }; //ninja.yell already assigned before ninja=null
var ninja = null; 

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

Program2

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 }; ////ninja.yell already assigned before ninja={}
var ninja = {}; //
assert( samurai.yell(4) == "hiyaaaa", "The method correctly calls itself." );//why this is working while the above code is working 

As you can see by these two examples, which are nearly same (according to my limited knowledge), the second is executing while the first is not. I've added descriptions of my question inside comments in the above code snippets.

To the point, my question is, in Program1 samurai.yell(4) is not executing. However, in Program2 samurai.yell(4) is executing. Both are nearly the same, but why does the second execute when the first does not?

解决方案

This has nothing to do with the difference between null and {}, it depends only on how the ninja.yell function is defined.

The first example doesn't work because inside the ninja.yell function, you are referring to ninja again:

return n > 0 ? ninja.yell(n-1) + "a" : "hiy"; 

So, if later on your are assigning null to ninja, this code will throw an error because null doesn't have a property yell.


The second example works because that's what you are exactly not doing. Instead of referring to the object that holds the function (ninja), you are giving the function a name and directly refer to that name:

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

The function is basically "self-contained", it doesn't depend on the values of free variables.

这篇关于javascript中的对象null和{}行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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