简体   繁体   English

在 Javascript 中,我们为什么要使用函数来创建对象?

[英]In Javascript, why do we use functions to make objects?

Going through some basic lessons on creating objects over at javascript.info.在 javascript.info 上学习一些关于创建对象的基本课程。

I'm trying to grasp on choosing which way to make objects.我试图掌握选择制作对象的方式。 Because there's so many ways to do it.因为有很多方法可以做到。 Also, why do we use functions to make objects?另外,我们为什么要使用函数来创建对象? And why do we use constructor functions to make objects when we can do without it?为什么我们可以不用构造函数来创建对象呢? In what situation one way is preferred over the other?在什么情况下,一种方式优于另一种方式?

Here's three ways:以下是三种方式:

1: 1:

let name = "bob";
let age = 24;

let user = {
  name: name,
  age: age,
}

alert(user.name);

2: 2:

function obj(name, age) {
return {
  name: name,
  age: age,
};
}

let user = obj("bob", 24);

alert(user.name);

3: 3:


function User(name, age) {
    this.name = name;
  this.age = age;
}

let user = new User("bob", "24")

alert(user.age);

isn't codes from 2 and 3 practically serve the same purpose of making a blueprint object? 2 和 3 中的代码实际上不是用于制作蓝图对象的相同目的吗? thanks.谢谢。

1 and 2 create same object which are instances of Object . 1 和 2 创建相同的对象,它们是Object实例。 3 creates an object which is Instance if User as well as Object . 3 创建一个对象,它是 Instance if User以及Object Using method 3, you can get achieve Inheritance or at least add common properties and methods to all the instances of that class/function (User).使用方法 3,您可以获得实现Inheritance或至少向该类/函数(用户)的所有实例添加通用属性和方法。 eg例如

 function User(name, age) { this.name = name; this.age = age; } User.prototype.showDetails = function () { console.log(`Name: ${this.name}\\nAge: E${this.age}`); } let user1 = new User("Bob", "24"); let user2 = new User("John", "25"); console.log(user1.showDetails()); console.log(user2.showDetails());

Option 1 creates a simple object.选项 1 创建一个简单的对象。 It's a sensible approach if you just want to store some key/data pairs.如果您只想存储一些密钥/数据对,这是一种明智的方法。

Option 2 lets you generate lots of similar objects with slight variations in data.选项 2 可让您生成大量相似的对象,但数据略有不同。

Option 3 is not very useful unless you combine it with a prototype so that the generated object has methods and makes use of inheritance.选项 3 不是很有用,除非您将它与原型结合起来,以便生成的对象具有方法并利用继承。

The Option 3 approach is superseded by class syntax .选项 3 方法被类语法取代。

Your option 2 and 3 is more like making a class than an object您的选项 2 和 3 更像是创建一个类而不是一个对象

The problem is, javascript only recently started defining things like classes and objects and instantiated objects.问题是,javascript 最近才开始定义类和对象以及实例化对象之类的东西。

For your enjoyment, here is way number 4为了您的享受,这里是第 4 种方式

class User{
  constructor (name, age) {
    this.name = name
    this.age = age
  }
}

let user = new User("bob", "24")

This new way allows you to extend another class, giving you proper inheritance.这种新方法允许您扩展另一个类,从而为您提供适当的继承。

class Person{
   constructor (gender) {
     this.gender=gender
   }
}
class User extends Person{
  constructor (name, age, gender) {
    super(gender)
    this.name = name
    this.age = age
  }
}

let user = new User("bob", "24", 'male')

The difference between 1 and 2 is negligible. 1 和 2 之间的差异可以忽略不计。

Number 1 is an object you've constructing using the {} syntax.数字 1 是您使用{}语法构建的对象。 Number two is a function that returns an object that you've made using the {} syntax.第二个是返回您使用{}语法创建的对象的函数。 The function is not part of the process here, it's simply returning a simple value, an object.该函数不是这里过程的一部分,它只是返回一个简单的值,一个对象。

EVERYTHING is an object in javascript.一切都是 javascript 中的对象。 Not actually, but mostly.实际上不是,但主要是。 Arrays are objects, these key value pairs you've made are an object (and each value in your object could also be set to an object).数组是对象,您创建的这些键值对是一个对象(并且对象中的每个值也可以设置为一个对象)。 Strings are objects, FUNCTIONS are objects.字符串是对象,FUNCTIONS 是对象。 Every non primitive datatype is an object.每个非原始数据类型都是一个对象。

Number 3 is a class.数字 3 是一个类。 Still an object, but with the properties of a class, including having an constructor.仍然是一个对象,但具有类的属性,包括具有构造函数。 An object is simply a value, and a data structure, with no explicit typing.一个对象只是一个值和一个数据结构,没有明确的类型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM