简体   繁体   English

构造函数 function 和 function 返回 object 之间有什么区别?

[英]What is the difference between a constructor function and function returning an object?

How are they different and why would you use one over the other?它们有何不同,您为什么要使用其中一种?

For example:例如:

 function setupOptionsObj(num) { return { data: num, func: function() { return num * 10 } } } function setupOptionsConstructor(num) { this.data = num; this.func = function() { return num * 10 } } var one = setupOptionsObj(123) var two = new setupOptionsConstructor(123) console.log(one.func()); console.log(two.func());

Thank you:)谢谢:)

setupOptionsObj() returns a plain object. setupOptionsObj()返回一个普通的 object。

new setupOptionsConstructor() returns an object whose prototype is setupOptionsConstructor.prototype . new setupOptionsConstructor()返回一个原型为 setupOptionsConstructor.prototype 的setupOptionsConstructor.prototype

Prototypes allow you to add methods and properties that apply to all objects created by that constructor.原型允许您添加适用于该构造函数创建的所有对象的方法和属性。 This is the basis of JavaScript's prototype-based object-oriented programming.这是 JavaScript 基于原型的面向对象编程的基础。

For example, below we only have one foo function, not one in every object.例如,下面我们只有一个foo function,而不是每个 object 中的一个。

 function setupOptionsConstructor(num) { this.data = num; } setupOptionsConstructor.prototype.func = function() { return this.data * 10 }; var two = new setupOptionsConstructor(123); var three = new setupOptionsConstructor(456); console.log(two.func()); console.log(three.func());

A constructor function is a normal function.一个构造函数 function 是一个普通的 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.这里的不同之处在于使用new运算符,它使 function 中的上下文 ( this ) 成为新实例,从而让它获取两个属性,并返回这个新实例。

Without the new operator, the context would have been the external one.如果没有new运算符,上下文将是外部的。

The whole magic, thus, is in the new operator:因此,整个魔法就在new 运算符中:

Let's take an example to understand the birth of constructors in Javascript.下面举个例子来了解Javascript中构造函数的诞生。 Suppose, you are asked to create an object of employee and it should have 4 properties firstName, lastName, gender, and designation.假设您被要求创建一个员工的 object,它应该有 4 个属性 firstName、lastName、gender 和 designation。 Well.出色地。 you said no problem.你说没问题。

var employee1={};
employee1.firstName="Anoop";
employee1.lastName="Rai";
employee1.gender="M";
employee1.designation="Software Engineer";

Above is the simplest way, first you created empty object and then you associated all the 4 properties to the object (of course, you could have also created the same via inline).上面是最简单的方法,首先创建空的 object,然后将所有 4 个属性关联到 object(当然,您也可以通过内联创建相同的属性)。 What if you are asked again to create another employee object with the same properties.如果再次要求您创建另一个具有相同属性的员工 object 怎么办。

var employee2={};
employee1.firstName="Ram";
employee1.lastName="Kumar";
employee1.gender="M";
employee1.designation="Associate Software Engineer";

Seems no problem at all.似乎完全没有问题。 Now what if you are asked that there are total 100 employees and you just created 2 of them, common you need to create another 98 employee objects.现在,如果你被问到总共有 100 名员工,而你刚刚创建了 2 名员工,通常你需要创建另外 98 个员工对象。 Now you won't be creating objects like above as it seems tedious.现在你不会像上面那样创建对象,因为它看起来很乏味。 Gotcha.明白了。 let's create a factory method that will be called any number of times and it will create objects and then return it to us.让我们创建一个工厂方法,该方法将被调用任意次数,它会创建对象,然后将其返回给我们。 Yeah!是的! write once and will be used many times.写一次,会被多次使用。

function createEmployeeObject(firstName, lastName, gender, designation){
  var employee={};
  employee.firstName=firstName;
  employee.lastName=lastName;
  employee.gender=gender;
  employee.designation=designation;
  return employee;
} 

var employee3=createEmployeeObject("Harry", "Dsouza", "M", "Project Manager");

Much convenient way, and without any duplicate codes.非常方便的方式,并且没有任何重复的代码。 Just call createEmployeeObject function with your arguments and in return you get your object.只需使用您的 arguments 调用 createEmployeeObject function,然后您将获得 object。 What if we have several types of objects say department.如果我们有几种类型的对象说部门怎么办。 Then also we will have a function that will create a department object and returns it.然后我们还将有一个 function 将创建一个部门 object 并返回它。

So, what is common in these kinds of functions.那么,这些功能的共同点是什么。 It is:-这是:-

  1. creating empty object var myObj={};创建空 object var myObj={};

  2. returning object after populating it return myObj;填充后返回 object 返回 myObj;

Creating empty object and returning object is common across all functions that create objects.创建空的 object 并返回 object 在所有创建对象的函数中都很常见。 Javascript has created a shortcut that lets you not to write these lines when you are using function that creates objects. Javascript 创建了一个快捷方式,当您使用创建对象的 function 时,该快捷方式允许您不要编写这些行。 So, these 2 lines can be skipped.因此,可以跳过这两行。 Way to do this is by using Constructors.做到这一点的方法是使用构造函数。

Using functions for creating objects is fairly common in Javascript, so Javascript provides shortcut that lets you write functions for creating objects.使用函数创建对象在 Javascript 中相当普遍,因此 Javascript 提供了快捷方式,让您可以编写创建对象的函数。 These special functions are called Constructor functions.这些特殊函数称为构造函数。 Constructors are functions that lets you populate the object which you need to create.构造函数是允许您填充需要创建的 object 的函数。

function createEmployeeObject(firstName, lastName, gender, designation){
  this.firstName=firstName;
  this.lastName=lastName;
  this.gender=gender;
  this.designation=designation;
}
var employee4=new createEmployeeObject("Alan", "Marks", "F", "Business Analyst");

You must know about this keyword It points to the current object.Remember that in Constructor functions Javascript creates empty object for us, so this actually points to that object only. You must know about this keyword It points to the current object.Remember that in Constructor functions Javascript creates empty object for us, so this actually points to that object only. Javscript Construtor functions automatically returns the object after it is populated. Javscript 构造函数在填充后自动返回 object。 Now how to tell Javascript that a function is called in Constructor mode, it's the new keyword that tells Javascript to treats a function as Constructor function. Now how to tell Javascript that a function is called in Constructor mode, it's the new keyword that tells Javascript to treats a function as Constructor function. Everytime you need an object, use new keyword and then call a function, and then that function prepares an object for us and returns it. Everytime you need an object, use new keyword and then call a function, and then that function prepares an object for us and returns it.

Even though Javascript is not class based, you must take care of the Constructor function name.即使 Javascript 不是基于 class 的,您也必须注意构造函数 function 名称。 it's not good to use camel case, use regular one.骆驼壳不好用,用普通的。

function Employee(firstName, lastName, gender, designation){
  this.firstName=firstName;
  this.lastName=lastName;
  this.gender=gender;
  this.designation=designation;
}
var employee5=new Employee("Mark", "Watson", "M", "DBA");

暂无
暂无

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

相关问题 JavaScript在构造函数和作为构造函数调用的函数返回对象之间有什么区别? - What difference is there in JavaScript between a constructor function, and function returning object which is invoked as a constructor? 在Durandal视图模型中返回函数和返回对象有什么区别? - What is the difference between returning a function and returning an object in a Durandal viewmodel? 原型对象和构造函数上的成员有什么区别? - What is the difference between members on prototype object and on constructor function? 函数和对象函数有什么区别 - What is the difference between function and object function 函数和构造函数之间的区别 - Difference between function and constructor function 函数构造函数和原型构造函数有什么区别? - What's the difference between a function constructor and prototype constructor? 构造函数创建后,构造函数中的方法与函数上的方法有什么区别 - What is difference between method in constructor and method on function after constructor created 在RequireJS中定义模块时,返回对象与返回init()函数之间有什么区别? - When defining module in RequireJS, what's the difference between returning an object VS returning an init() function? 函数构造函数对象与对象内部对象之间的javascript区别 - javascript difference between function constructor object and an object inside object 函数对象和可调用对象之间有什么区别? - What is the difference between a function object and a callable object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM