简体   繁体   English

JavaScript 中 object 的区别

[英]Difference between object in JavaScript

Im doing a course in frontend Dev in uni and the teacher insists on us using a old book so to learn the history and basics of JavaScript before we move on to more advanced and recent implementations.我在大学做前端开发课程,老师坚持让我们使用一本旧书,以便在我们继续进行更高级和最近的实现之前了解 JavaScript 的历史和基础知识。

Now in this book we are instructed to code a webpage for a food truck and it is supposed to take orders.现在在这本书中,我们被指示为食品卡车编写一个网页,它应该接受订单。

Now in some scripts the objects are defined like this:现在在一些脚本中,对象的定义如下:

function DataStore() {
    this.data = {};
  }

Here the data object is defined using the keyword "this" as in saying it belongs to the function object DataStore.这里数据 object 是使用关键字“this”定义的,因为它属于 function object DataStore。

however in some scripts the data object is defined as:但是在某些脚本中,数据 object 定义为:

FormHandler.prototype.addSubmitHandler = function() {
    console.log('Setting submit handler for form');
    this.$formElement.on('submit', function(event){
      event.preventDefault();

      var data = {};

My question is what is the difference in the two data objects?我的问题是这两个数据对象有什么区别?

Short answer is at the bottom简短的答案在底部

Long and boring introduction to how things work对事物如何运作的冗长而无聊的介绍

When you write this:当你写这个:

function SomeThing() { }

You can always do你总能做到

let a = new SomeThing();

even when it doesn't make sense like in:即使它没有意义,例如:

function lel() { console.log('lelelel'); }
let g = new lel();
console.log(g);
console.log(g.constructor.name);

What this means is that classes are actually the same as functions.这意味着类实际上与函数相同。 And a function in which you use the keyword this usually means you will want to create instances of it.在 function 中使用关键字this通常意味着您需要创建它的实例。

now if I want all instances of my lel() function class to have a property called foo and a method called bar here's how you do:现在,如果我希望我的 lel lel() function class 的所有实例都有一个名为foo的属性和一个名为bar的方法,请执行以下操作:

lel.prototype.foo = "Some initial value";
lel.prototype.bar = function() {
  console.log(this.foo);
}

now I can do现在我可以做

let g = new lel();
lel.bar();
lel.foo = "Hell yeah !";
lel.bar();

In conclusion, this:总之,这是:

function SomeThing() {
  this.data = {};
}

SomeThing.prototype.setData = function(key, value) {
  this.data[key] = value;
}

SomeThing.prototype.getDataKeys = function() {
  return Object.keys(this.data);
}

SomeThing.prototype.getDataValues = function() {
  return Object.values(this.data);
}

is the same thing as this和这个是一样的

class SomeThing {
  constructor() {
    this.data = {};
  }

  setData(key, value) {
    this.data[key] = value;
  }

  getDataKeys() {
    return Object.keys(this.data);
  }

  getDataValues() {
    return Object.values(this.data);
  }
}

Clarifications about your question澄清你的问题

If somewhere in your code you have:如果您的代码中某处有:

FormHandler.prototype.addSubmitHandler = function() {
    console.log('Setting submit handler for form');
    this.$formElement.on('submit', function(event){
      event.preventDefault();

      var data = {};

if necessarily means that somewhere else in your code you have如果必然意味着您的代码中的其他地方有

function FormHandler(...) { ... }

Short answer简短的回答

This:这个:

function DataStore() {
  this.data = {};
}

is how you define a class named DataStore with a property called data initialized to the value {}是您如何定义一个名为DataStore的 class 并使用一个名为data的属性初始化为值{}

And this:和这个:

FormHandler.prototype.addSubmitHandler = function() {
    ...
    var data = {};
}

is how you add a method called addSubmitHandler to the already defined class FormHandler .是如何向已定义的 class FormHandler添加一个名为addSubmitHandler的方法。 That method uses a local variable called data , could have been any other name该方法使用名为data的局部变量,可以是任何其他名称

In the first case, data is a property of the object that is created like this: new DataStore .在第一种情况下,数据是 object 的一个属性,它的创建方式如下: new DataStore You can access this property like this:您可以像这样访问此属性:

var obj = new DataStore();
obj.data // => {}
/* or */
obj['data'] // => {}

In the second case, data is just a global variable, inside of an event handler, that is added executing the function.在第二种情况下,数据只是事件处理程序内部的一个全局变量,它是在执行 function 时添加的。

var obj = new FormHandler();
obj.addSubmitHandler();

You access this variable like this:您可以像这样访问此变量:

data // => {}

I don't think it's a good idea to learn old JS.我认为学习旧的 JS 不是一个好主意。 You would be out of date.你会过时的。 You wouldn't be able to use latest technologies, and it would be harder to get a job.你将无法使用最新的技术,而且更难找到工作。

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

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