I have a really rookie question....
If I define a JS object, and I use a function to generate a property value, why does the property value return a function instead of the generated value?
Example:
var object = {
bla: 1,
days: [],
test : function(){
return 'bla';
}
}
console.log(object.test);
I would expect object.test
to be 'bla'. Instead it's function(){ return 'bla'; }
... Why?
you have to execute that function, in this way: console.log(object.test());
or, as pointed out by @YuriiKovalenko, you can write the function like this:
var object = {
bla: 1,
days: [],
test : (function(){ return 'bla'; })()
}
and then console.log(object.test);
will give you "bla"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다