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

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

问题描述

这位博客作者正在说下面的函数是构造函数

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

var catC =新猫(蓬松,白色);

Cat 函数的实例具有name和颜色属性。这是普通函数和构造函数之间的唯一区别吗?

解决方案

构造函数是一个普通的函数。
$ b

这里的区别在于使用 new 运算符,它使上下文( this

如果没有 new <$ c $>,那么新实例的函数会返回这个新实例。 / code>运算符,如果代码在松散模式下位于全局范围中,则上下文应该是外部的( window >) undefined 如果在严格模式下)。



也就是说,如果您省略 new

  var catC = Cat(Fluffy,White); 

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




  • catC 未定义

  • name color 现在属性的外部范围



整个魔术在 new operator


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

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



使用指定的参数调用
构造函数foo,并将
绑定到新创建的对象。新的foo相当于新的
foo(),即如果没有指定参数列表,则会调用foo而不使用
参数。



返回的对象由构造函数变成全新表达式的
结果。如果构造函数
没有显式地返回一个对象,那么在步骤1中创建的对象就是
。 (通常构造函数不返回值,但是如果他们想要覆盖普通对象
的创建过程,
可以选择这样做。)


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


In this blog author is saying below function is constructor function

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

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

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

解决方案

A constructor function is a normal function.

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.

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).

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 is undefined as your function returns nothing
  • name and color are now properties of the external scope

The whole magic, thus, is in the new operator :

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

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

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.

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.)

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天全站免登陆