[英]why does console.log show different description
I run the following in node: why does the first console.log show: 我在节点中运行以下命令:为什么第一个console.log显示:
greet: [Function],
and the second console.log show: 第二个console.log显示:
greet: [Function: greet]
Is there any difference? 有什么区别吗? (note, console.log in chrome doesn't show the difference) (请注意,Chrome中的console.log没有显示出区别)
function deepCopy(obj){ /* an object has key value pairs check if val is object if true deepCopy(val) else return value */ const keys = Object.keys(obj); // gets keys of object const newObject = {} for(index in keys){ if(typeof(obj[keys[index]]) === 'object') { newObject[keys[index]] = deepCopy(obj[keys[index]]) }else{ newObject[keys[index]] = obj[keys[index]] } } return newObject } o={ name:'David', teach:true, obj:{ greet:function(){ console.log('hi!')}, last:'cross' } } o2 = deepCopy(o) o.obj.greet=function(){ console.log('bye')} o.name='george' console.log(o) console.log(o2) o.obj.greet() o2.obj.greet()
{ great:function(){} }
, it will have a .name
and will be print out by console.log
. 当您声明方法{ great:function(){} }
,它将具有.name
并由console.log
打印出来。 Even you pass it around (assign to o2
via deepCopy
), it will keep it name. 即使您将其传递(通过deepCopy
分配给o2
),它也会保留其名称。 function(){}
, it's .name
is empty. 创建匿名函数function(){}
,其.name
为空。 which you assign it to o.great = function(){}
. 您将其分配给o.great = function(){}
。 I create a simplify version to explain op question to test. 我创建了一个简化版本以解释要测试的操作问题。
const a = { great: function(){} }; // in object declaration, method great as name "great" (x) const b = {}; b.great = function(){}; // b.great is point to an anonymous function with no name (y) // When console.log a function, if function has name, it will show out console.log("a.great", a.great); console.log("b.great", b.great); console.log("a.great.name", JSON.stringify(a.great.name)); // "great" console.log("b.great.name", JSON.stringify(b.great.name)); // ""; b.great = a.great; // simliar when you deep copy o2 from o // in this case, b.great is point to the method great (x) // so when you log out the name, it will print out console.log("b.great.name", JSON.stringify(b.great.name)); // "great" a.great = function(){}; // similar when o.great get re-assign // a.great got re-assign to an anonymous function with no name (z) // so when you log out the name, it will print out "" console.log("a.great.name", JSON.stringify(a.great.name)); // "" // Finally: console.log(a.great); console.log(b.great);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.