简体   繁体   English

var a = {}和var a = function(){}有什么区别

[英]what is the difference between var a = {} and var a = function(){}

I got confused about these two , var a = {} and var a = function(){} 我对这两个感到困惑,var a = {}和var a = function(){}
a is an object in the first case , a is a function in the second case. 在第一种情况下,a是一个对象,在第二种情况下,a是一个函数。 And function is also a object. 功能也是一个对象。 what is the difference? 有什么区别?

A function is a type of an object in Javascript, but a empty function ( function(){} ) is very different from an empty object ( {} ). 函数是Javascript中对象的一种类型,但是空函数( function(){} )与空对象( {} )有很大不同。 The easiest way to see the difference is to just execute them and see what they do differently. 观察差异的最简单方法是只执行它们,看看它们有何不同之处。 Using the console you can play around and see how they are different. 使用控制台,您可以玩转并查看它们的不同之处。

  var emptyFunction = function() {} console.log(emptyFunction) // function(){} console.log(typeof emptyFunction) //"function" var emptyObject = {} console.log(emptyObject) //Object{} console.log(emptyObject) // "object" emptyFunction() // returns undefined because your function has no return emptyObject() // Uncaught TypeError: object is not a function 

The first thing is the type of them are different things as the typeof operator indicates. 第一件事是它们的类型是不同的事物,如typeof运算符所示。 It shows you what Javascript thinks of the type The last line is where you really start to see the difference. 它向您展示Javascript对类型的看法。最后一行是您真正开始看到差异的地方。 A function is able to be invoked by using () to call it. 可以使用()调用函数。 An object does not have that ability and it will cause an error because the type Object doesn't have a behavior defined for it that involves using the () 一个对象不具有该功能,并且将导致错误,因为类型Object没有定义涉及使用()

As you pointed out though, a function is just a specific type of object so it can do the same sort of things an object. 正如您所指出的,函数只是对象的一种特定类型,因此它可以对对象执行相同的操作。 So we can do something like this: 所以我们可以做这样的事情:

emptyFunction.foo = function(){ return 'foo';} console.log(emptyFunction.foo()) //'foo' emptyObject.bar = function(){ return 'bar';} console.log(emptyObject.bar()) //'bar'

As you can see when it comes down to it, the difference is a function is a specialized object that can be invoked. 如您所见,函数是一个可以调用的专用对象。 You can read more about functions in Javascript here 您可以在此处阅读有关Javascript函数的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 $(var)和var有什么区别? - What is the difference between $(var) and var? 'var webApp = {..}'和'var webApp = function(){..}'之间的区别是什么? - What's the difference between 'var webApp = { .. }' and 'var webApp = function (){ .. }' 函数中的this和var之间的区别 - Difference between this and var in a function 在JavaScript中,var a = 5有什么区别。 而var a = 5? - In JavaScript, what is the difference between var a=5. and var a=5? var myVar =“”和有什么不一样? 和var myVar ;? - what is the difference between var myVar = “”; and var myVar;? "let" 和 "var" 和有什么不一样? - What is the difference between "let" and "var"? JavaScript 中的 (var1 !== var2) 与 (var1 && var1 !== var2) 有什么区别? - What is the difference between (var1 !== var2) vs (var1 && var1 !== var2) in JavaScript? 在javascript中,window.function(){}和var variable = function有什么区别? - In javascript, what is the difference between window.function(){} and var variable = function? JavaScript中的var thing和function thing()有什么区别? - What is the difference between var thing and function thing() in JavaScript? 使用var与函数声明javascript对象有什么区别? - What is the difference between declaring javascript objects with var vs. with function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM