每个JavaScript对象都是一个函数吗? [英] Is every JavaScript Object a function?
问题描述
是否有JavaScript对象不是函数?
javascript:x = y = z = Object; alert([window.navigator.userAgent,x,y,z] .join(\\\
\\\
))
(有评论那 y
, z
case Object
函数Object(){...}
的引用,因为对象
的值被分配给 x
,它们是相同的,作为证明
javascript:x = Object; xp = 43; alert([x == Object,x === Object,xp,Object.p])
显示
true ,true,43,43
给定函数Thing(){} code>确实
x = new Thing()
make x
对象或对象的引用? new Thing()
和 Thing
?或者 y
in y = x = new Thing()
或 y = x = Thing
?如果 >事情=函数(){}
?这个区别是没有意义的。 一切(或是否?)是通过引用来调用的,但是可以通过评估字符串来强制调用名称。所以...)
javascript:
void function(x,y,z){
alert ([window.navigator.userAgent,x,y,z] .join(\\\
\\\
))
(Object,Object,Object)
或
javascript:
void function(x){(function(y){(function(z){
alert([window.navigator.userAgent,x,y,z] .join(\\\
\\\
))
$ b $(很好) moot - 必须使用(...)
或来强制函数
的值。(...)
的细微差别很微妙: javascript:/ * 43.p给出了一个运行时错误,但不是...... * /
alert([(43).p = 34,43 [q] = 17,(x = 43).z =hmmm]);
alert([43 [p],(43).z,xp,x [z],x]);
显示 34,17,hmmm
和 ,,,, 43
)
甚至是一个ar物体对象
javascript:alert([window.navigator.userAgent,Object,Object,Object] .join(\ n $ \\ n))
给出:
Mozilla / 5.0(X11; U; Linux i686; EN-US; rv:1.9.2.3)Gecko / 20100423 Ubuntu / 10.04(lucid)Firefox / 3.6.3
function Object(){
[native code]}
function Object(){
[native code]}
function Object(){
有许多对象不是Object。
正如在答案,对象可能不是自身如果它被修改。
危险!危险! Will Robinson!
x = y = z = Object = null;警报([window.navigator.userAgent,对象,X,Y,Z]。加入( \\\
\\\
));
引用
解决方案您没有创建对象,您创建了对对象的引用功能。如果你想让这些东西成为对象,你可以这样做:
x = y = z = {}
code>
然后 alert(x)
将返回对象(希望)包含注释 - 默认情况下为 Object
。
是构造对象的函数。如果你重新命名Object(Firefox至少似乎允许我,并没有测试过所有的浏览器),那么 Object
将不再是一个Function,它会是你分配给它。那么,答案是否,Object不是始终是Function,而是应该是,除非它已被明确地重新声明。根据Firebug的说法:
>>> Object
Object()
>>> Object = {}
Object {}
>>> Object
Object {}
看起来它可以被重新分配。如果有的话,我不能保证会有什么样的影响。
Is there a JavaScript Object that is not a function?
javascript: x=y=z=Object; alert([window.navigator.userAgent,x,y,z].join("\n\n"))
(There was a comment that x
,y
,z
are merely references in which case Object
is also merely a reference to function Object(){ ... }
because Object
's value is assigned to x
and they are the "same". As "proof"
javascript:x=Object;x.p=43;alert([x==Object,x===Object,x.p,Object.p])
displays
true,true,43,43
Given function Thing(){}
does x=new Thing()
make x
an object or a reference to one? What about new Thing()
and Thing
? Or y
in y=x=new Thing()
or y=x=Thing
? What if Thing=function(){}
? The distinction is moot. "Everything" (or is it?) is called-by-reference but call-by-name can be coerced by evaluating strings. So ...)
javascript:
void function(x,y,z){
alert( [window.navigator.userAgent,x,y,z].join("\n\n") )
}(Object,Object,Object)
or
javascript:
void function(x){ (function (y){ (function (z){
alert( [window.navigator.userAgent,x,y,z].join("\n\n") )
})(y) })(x) }(Object)
(well not quite moot - the function
's values must be coerced using (...)
or void
. The nuances of (...)
are subtle:
javascript: /* 43.p gives a runtime error but not ... */
alert([ (43).p=34, 43["q"]=17, (x=43).z="hmmm" ]);
alert([ 43["p"], (43).z, x.p, x["z"], x]);
displays 34,17,hmmm
and ,,,,43
)
or even an array of Objects
javascript:alert([window.navigator.userAgent,Object,Object,Object].join("\n\n"))
gives:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3
function Object() {
[native code] }
function Object() {
[native code] }
function Object() {
[native code] }
There are many objects that are not Object.
As pointed out in one of the answers, Object may not be itself IF it is modified.
Danger! Danger! Will Robinson!
x=y=z=Object=null; alert([window.navigator.userAgent,Object,x,y,z].join("\n\n"));
references
- Object and Function are quite confusing
- Difference between a constructor and an Object
- Is Function really an Object
- Is JavaScript function a "function" or an "object" or both?
- Every Object is a function and every function is Object - Which is Correct?
- Why in JavaScript is a function considered both a constructor and an object?
解决方案 You didn't create objects, you created references to the Object function. If you wanted those to be objects you could do this:
x = y = z = {}
Then alert(x)
will return object [Object]
.
To (hopefully) encompass the comments - by default Object
is a Function which constructs Objects. If you reassign the name Object (Firefox at least seems to allow me to, haven't tested all browsers) then Object
will no longer be a Function, it will be whatever you assigned to it. So then, the answer is "no", Object is not always a Function, but should be unless it has been explicitly re-declared. According to Firebug:
>>> Object
Object()
>>> Object = {}
Object {}
>>> Object
Object {}
Seemingly it can be reassigned. I cannot vouch for what kind of impacts that would have, if any.
这篇关于每个JavaScript对象都是一个函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!