JavaScript中的函数和构造函数之间有什么区别? [英] What are all the differences between function and constructor function in JavaScript?

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

问题描述

博客作者中,下面的功能是构造函数

In this blog author says below function is a constructor function:

function Cat(name, color) {
  this.name = name;
  this.color = color;
}
Cat.prototype.age = 0;

var catC = new Cat("Fluffy", "White");

Cat 函数的实例具有名称和颜色属性。

The instances of Cat function has a name and colour property. Is this the only difference between normal and constructor function?

推荐答案

构造函数是普通函数吗?

A constructor function is a normal function.

这里的区别在于使用 new 运算符使上下文( this )在函数中创建新实例,从而使其具有两个属性,并返回此新实例。

What makes the difference here is the use of the new operator which makes the context (this) in the function the new instance, thus letting it take the two properties, and returns this new instance.

不包含 new 运算符,该上下文将是外部的(如果您的代码处于松散模式下的全局范围内,则该窗口为 window 未定义(如果在严格模式下)。

Without the new operator, the context would have been the external one (window if your code is in the global scope in loose mode, undefined if in strict mode).

也就是说,如果您省略 new

That is, if you omit the new

var catC = Cat("Fluffy", "White");

函数有效(如果您不处于严格模式),但是有两个不同的结果:

the function "works" (if you're not in strict mode) but you have two different results :


  • catC 未定义由于您的函数什么都不返回

  • 名称 color 现在已成为属性

  • catC is undefined as your function returns nothing
  • name and color are now properties of the external scope

因此,整个魔术就在新操作符


当执行代码new foo(...)时,会发生以下事情:

When the code new foo(...) is executed, the following things happen:

创建了一个新对象,该对象继承自foo.prototype。

A new object is created, inheriting from foo.prototype.

使用指定的参数调用
构造函数foo,并将
绑定到新创建的对象。 new foo等效于new
foo(),即,如果未指定参数列表,则在没有
参数的情况下调用foo。

The constructor function foo is called with the specified arguments and this bound to the newly created object. new foo is equivalent to new foo(), i.e. if no argument list is specified, foo is called without arguments.

返回的对象构造函数的作用成为整个新表达式的
结果。如果构造函数
没有显式返回对象,则使用在步骤1中创建的对象
代替。 (通常,构造函数不返回值,但是如果他们想覆盖普通对象
的创建过程,则
可以选择这样做。)

The object returned by the constructor function becomes the result of the whole new expression. If the constructor function doesn't explicitly return an object, the object created in step 1 is used instead. (Normally constructors don't return a value, but they can choose to do so if they want to override the normal object creation process.)

当我说这是我的正常功能时,我省略了一件事:开发人员的意图。您通常将函数定义为称为构造函数(即使用 new )。在第一种情况下,您通常会使用参数来初始化实例的字段(使用 this.name = ... ),并且通常会在原型中添加函数(就像您所做的那样),以便它们可用于所有实例。为了明确您的意图,习惯上以大写字母开头来命名构造函数。

When I said it's a normal function I, I omitted one thing : the intent of the developer. You usually define functions to be either called as constructors (i.e. with new) or not. In the first case you most often use the arguments to initialize the fields of the instance (using this.name = ...) and you often follow by adding functions to the prototype (as you did) so that they become available for all instances. And to make your intent clear, it's customary to name your constructor starting with an uppercase letter.

这篇关于JavaScript中的函数和构造函数之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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