为什么一些JavaScript构造函数不是函数? [英] Why are some JavaScript constructors not functions?

查看:117
本文介绍了为什么一些JavaScript构造函数不是函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

澄清:

JavaScript构造函数应更适当地写为JavaScript构造函数,以强调所考虑的构造函数不仅仅是原生的JavaScript语言构造函数,如Object,Array,Function,等等,而是其他的,对于JavaScript语言定义是外在的,但是对于浏览器是固有的,例如 XMLHttpRequest 。词语JavaScript意在指示这些构造函数被表达和访问使用JavaScript。



一些引用:





修辞地,有对构造函数



(方便的,这是因为Objects ARE函数,而函数是对象!

< a href =http://stackoverflow.com/questions/372202/why-in-javascript-is-a-function-considered-both-a-constructor-and-an-object>为什么在JavaScript中是一个被考虑的函数一个构造函数和一个对象?

更具体地说,对象,或者是obj-eggs?,ARE,忽略字面实例,函数和函数的实例化是函数的Object实例。有争议的是,函数是对象的存在的根本,由事实证明

7. 功能

     

8. 使用对象

,位于MDN文档 JavaScript指南中。 )



为什么接口DOM的构造函数不能正常工作?

  javascript:
alert([
使用浏览器环境:\\\
+ window.navigator.userAgent,
Option,Image,Audio,
Storage,XMLHttpRequest,Worker,FileReader,
]。join(\\\
\\\
));

显示我们:


使用浏览器环境:

Mozilla / 5.0(X11; U; Linux i686; en-US;
rv:1.9.2.3)Gecko / 20100423 Ubuntu / 10.04 /3.6.3



[object Option]



[object Image]



[object Audio]



[object Storage]



[object XMLHttpRequest] / p>

[object Worker]



[object FileReader]


但... ...

  javascript:
alert([
XPCNativeWrapper,
] .join(\\\
\\\
));

(产生


函数XPCNativeWrapper(){
[native code]}




和JavaScript语言构造函数 ARE函数。 / p>

  javascript:
alert([
使用浏览器环境:\\\
+ window.navigator。
数组,布尔,日期,函数,
数字,对象,RegExp,String,
错误,迭代器,
] .join(\\\
\\\
));

为我们提供:


使用浏览器环境:

Mozilla / 5.0(X11; U; Linux i686; en-US;
rv:1.9.2.3)Gecko / 20100423 Ubuntu / 10.04 /3.6.3



function Array(){
[native code]}



function Boolean (){
[native code]}



function Date(){
[native code]}



function Function(){
[native code]}



/ p>

function Object(){
[native code]}



function RegExp(){
[native code]}



function String(){
[native code]}



function Error(){
[native code]}



function Iterator(){
[native code]}



解决方案

第一个:


对象ARE函数


否,不是:

 > a = function(){} 
function(){}
> a instanceof Object
true
> b = {}
Object
> b instanceof函数
false






toString 方法(这是当你执行字符串连接时调用)不是一个可靠的方法来获取对象的信息。如果我使用 typeof ,我会得到以下结果:

 
Mozilla / 5.0(Macintosh; Intel Mac OS X 10.7; rv:5.0.1)Gecko / 20100101 Firefox / 5.0.1

函数

function

函数

对象

函数

函数

函数

所以你看到,其中大部分,除了 Storage > 实际上是函数(为什么它不适用于 Storage ,我不知道)。



另请注意,DOM接口的行为可能与本机JavaScript对象不同。



另一方面,在 Chrome c $ c> toString 方法给出:

  [object Function] 

[object Function]

[object Function]

函数Storage(){[native code]}

function XMLHttpRequest本地代码]}

函数Worker(){[native code]}

function FileReader(){[native code]}


Clarification:
"JavaScript constructor" should be more properly be written as "javascript constructor" to emphasize that the constructors considered are not just the native JavaScript language constructors, such as Object, Array, Function, etc. but also others, extrinsic to the JavaScript language definition but intrinsic to a browser, such as XMLHttpRequest, The word "JavaScript" is meant to indicate these constructors are expressed and accessed using JavaScript.

some references:

Rhetorically, there are references to constructor functions but NOT constructor objects!

(Facetiously, this is because Objects ARE functions, and Functions are objects!
Why in JavaScript is a function considered both a constructor and an object?
More specifically, objects, or is that obj-eggs?, ARE, ignoring literal instances, instantiations of functions and functions are Object instances of Functions. It is arguable that functions are fundamental to the existence of objects as evidenced by the fact
7. Functions
      precedes
8. Working with Objects
in the MDN docs JavaScript Guide. That section 8, I object!, provides the details needed to create objects using constructors and function instantiations!)

Why are constructors that interface the DOM not functions?

javascript:
  alert([
    "using browser environment:  \n"+window.navigator.userAgent,
     Option, Image, Audio,
       Storage, XMLHttpRequest, Worker, FileReader,
   ] . join("\n\n"));

shows us:

using browser environment:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3

[object Option]

[object Image]

[object Audio]

[object Storage]

[object XMLHttpRequest]

[object Worker]

[object FileReader]

but ...

javascript:
  alert([
             XPCNativeWrapper,
  ].join("\n\n"));

(which produces

function XPCNativeWrapper() { [native code] }

)

and JavaScript language constructors ARE functions.

javascript:
  alert([
    "using browser environment:  \n"+window.navigator.userAgent,
             Array, Boolean, Date, Function,
               Number, Object, RegExp, String,
                 Error, Iterator,
  ].join("\n\n"));

gives us:

using browser environment:
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 Array() { [native code] }

function Boolean() { [native code] }

function Date() { [native code] }

function Function() { [native code] }

function Number() { [native code] }

function Object() { [native code] }

function RegExp() { [native code] }

function String() { [native code] }

function Error() { [native code] }

function Iterator() { [native code] }

解决方案

First:

Objects ARE functions

No, the are not:

> a = function() {}
  function () {}
> a instanceof Object
  true
> b = {}
  Object
> b instanceof Function
  false


The toString method (which is what gets called when you do string concatenation) is not a reliable way to get information about an object. If I use typeof, I get the following:

using browser environment:  
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:5.0.1) Gecko/20100101 Firefox/5.0.1

function

function

function

object

function

function

function

So you see, most of them, apart form Storage, are actually functions (why it does not work for Storage, I don't know).

Also keep in mind that the DOM interface can behave differently than native JavaScript objects.

On the other hand, in Chrome the toString method gives this:

[object Function] 

[object Function] 

[object Function] 

function Storage() { [native code] } 

function XMLHttpRequest() { [native code] } 

function Worker() { [native code] } 

function FileReader() { [native code] }

这篇关于为什么一些JavaScript构造函数不是函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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