JS中具有默认属性和类对象的数据结构是什么? [英] What is this data structure with default property and type-like object in JS?

查看:57
本文介绍了JS中具有默认属性和类对象的数据结构是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在某些JS对象中,它们具有带有某些特殊行为的属性.例如,在Sequelize中,如果将模型对象登录到控制台,则会看到该对象包含诸如 _data _changed _modelOptions

I notice that in some JS objects, they have properties with some special behaviours. For example, the model objects in Sequelize, if I log a model to the console, I see that the object contains properties such as _data, _changed, _modelOptions, etc.

但是,访问对象本身时, _data 属性似乎是其默认属性.例如:

However, when accessing the object itself, the _data property appears to be its default property. For instance:

const userModel = UserModel.findOne(...);
console.log(userModel.email);   // this prints the email field of the record
console.log(userModel._data.dataValues.email)  // this also prints the email of the record

看来我不必从 _data.dataValues 访问电子邮件.我可以直接从 userModel 对象访问它.

It appears that I don't have to access the email from _data.dataValues. I can access it directly from the userModel object.

当我打印整个对象时,我还注意到 _data.dataValues 中的值被打印了.

When I print the whole object, I also notice that the values in _data.dataValues get printed.

与此:

console.log(JSON.stringify(userModel))

我将得到以下结果:

{
   name: 'John',
   email: 'john@smith.com'
}

但是,这个:

console.log(userModel)

我会得到这个:

t {
   _data: user {   // what is that 'user'  before the object? is it a type definition?
      dataValues: {
         name: 'John',
         email: 'john@smith.com'
      }
      _previousDataValues: {
         name: 'John',
         email: 'john@smith.com'
      }
      _modelOptions: {
         ...
      }
      ...
   }
}

这看起来与通常的JS对象有点不同,因为它似乎具有对象的类型",即这些属性是内部"的,并且在打印出来时不可见.

This looks a little different from the usual JS object in that it seems to have a "type" of sort to the object those properties are "internal" and not visible when printed out.

我以为一开始是一个类,但是我尝试打印我创建的一个类,并将输出与该模型的控制台输出进行比较,它们看起来有所不同.

I thought it was a class at first but I tried printing a class I've created and compared the output with the console output from this model and they look different.

我不经常在JS中看到这种数据结构.JS和Node中的这种数据结构具体是什么?与JS中的常规对象相比,此特殊"对象有什么不同和有用?

I don't see this data structure in JS often. What is this data structure in JS and in Node specifically? What is different and useful about this "special" object compared to the regular object in JS?

推荐答案

实现这些行为形式没有什么特别的.使用 Object可以轻松完成此操作.defineProperty Object.defineProperties .

Achieving these form of behaviour is nothing special. This can be done easily using Object.defineProperty or Object.defineProperties.

这是一个例子

function Person(fName, lName) {
  this._data = {
    fName: fName,
    lName: lName
  }
  
  Object.defineProperties(this, {
    fName: {
      get : function () {
        return this._data.fName;
      },
      set: function (value) {
        this._data.fName = value;
      }
    },
    lName: {
      get : function () {
        return this._data.lName;
      },
      set: function (value) {
        this._data.lName = value;
      }
    }
  });  
}

const ratul = new Person("Ratul", "sharker");

console.log(ratul);

console.log(ratul.fName);
ratul.fName = "Ra2l";
console.log(ratul.fName);

此处,默认情况下,将 enumerable 属性设置为 false (请检查定义属性文档.)如果将其设置为 true ,则它将出现在 console.log(ratul)中.

Here the enumerable property is by default set to false (check define properties documentation.) If you set it to true then it will appear in console.log(ratul).

这些行为在续集中的主要用法是跟踪价值变化.直接从对github进行.

Primary usage of these sort of behaviour in sequelize is to keep track of value changes. Directly from Sequelize github.

setDataValue(key, value) {
    const originalValue = this._previousDataValues[key];

    if (!_.isEqual(value, originalValue)) {
      this.changed(key, true);
    }

    this.dataValues[key] = value;
  }

跟踪数据值变化的最明显的原因是在调用 Model.save 时,然后sequelize可以优化哪些属性/属性应将对db的写续性.

Most obvious reason to track data value changes is to while calling Model.save, then sequelize can optimise which attributes/properties should sequelize write to db.

这是 对象正在使用.defineProperty ,该声明在 refreshAttributes 中声明,该代码从 init 中调用.

This is where Object.defineProperty is being used, which is declared within refreshAttributes, which is called from init.

Object.defineProperty(this.prototype, key, attributeManipulation[key]);

这篇关于JS中具有默认属性和类对象的数据结构是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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