JS 中的 OOP object.instance.function 和 OOP object.function 有什么区别?

发布于 2025-01-15 07:37:50 字数 2673 浏览 2 评论 0原文

我想理解如果我在调用内部的键/实例之前从包含“实例”的OOP中调用函数,那么在JS中会有什么差异

解决后编辑:

我需要将该类用作单例,但正如下面的 @VLAS 答案,当您将类导出为默认类并添加新的时,它会自动使其成为单例。这是语法:

class Person {
    ...
}
export default new Person() //this syntax should make it singleton

编辑之前的代码用于解释我的问题:

import Person from './personManager';
const person = new Person()
person.introduceSelf()
person.instance.introduceSelf()

我认为它是 ES5 的语法,现在在 ES6 中我们不再需要了?或者有更复杂的东西我不明白?

在我们的项目中,我们有这段代码,我刚刚添加了 personManager.js 作为示例:

syngletonSymbol.js

/* eslint-disable */
let SingletonSymbol;
if (!SingletonSymbol) {
  SingletonSymbol = (function (Object) {
    let ObjectPrototype = Object.prototype,
      defineProperty = Object.defineProperty,
      prefix = `__simbol${Math.random()}__`,
      id = 0;

    function get() { /* avoid set w/out get prob */ }

    function SingletonSymbol() {
      const __symbol__ = prefix + id++;
      defineProperty(
        ObjectPrototype,
        this._ = __symbol__,
        {
          enumerable: false,
          configurable: false,
          get, // undefined
          set(value) {
            defineProperty(this, __symbol__, {
              enumerable: false,
              configurable: true,
              writable: true,
              value
            });
          }
        }
      );
    }

    defineProperty(SingletonSymbol.prototype, 'toString', {
      enumerable: false,
      configurable: false,
      writable: false,
      value: function toString() {
        return this._;
      }
    });

    return SingletonSymbol;
  }(Object));
}

export default SingletonSymbol;

singleton.js

/* eslint-disable */
const SingletonSymbol = require('./singletonSymbol');

export default class Singleton {
  static get instance() {
    if (!this[SingletonSymbol]) {
      this[SingletonSymbol] = new this();
    }

    return this[SingletonSymbol];
  }

  constructor() {
    const Class = new.target ? new.target : this.constructor;

    if (!Class[SingletonSymbol]) {
      Class[SingletonSymbol] = this;
    }

    return Class[SingletonSymbol];
  }
}

personManager.js

import Singleton from 'infrastructure/helpers/singleton';
class Person extends Singleton{
  name;
  constructor() {
    super()
    this.name = 'John';
    this._init();
  }
  _init = () => {
      this.name = 'Mario';
  }
  introduceSelf() {
    console.log(`Hi! I'm ${this.name}`);
  }

}

我会感谢任何有助于我理解这个想法的答案,以及所有单例是否已被弃用并且可以从我的项目中删除

I would like to understand what will be the difference in JS if I will call a function from an OOP including "instance" before calling the key/instance inside.

EDIT after solved:

I need to use the class as singleton but as @VLAS answer below when you export the class as default and add new it will make it singleton automatically. here is the syntax:

class Person {
    ...
}
export default new Person() //this syntax should make it singleton

the code before the edit for the explain of my issue:

import Person from './personManager';
const person = new Person()
person.introduceSelf()
person.instance.introduceSelf()

I think its a syntax from ES5 that now in ES6 we are don't need any more? or there is something more complex that I didn't understand?

in our project we have this code I just added the personManager.js for the example:

syngletonSymbol.js

/* eslint-disable */
let SingletonSymbol;
if (!SingletonSymbol) {
  SingletonSymbol = (function (Object) {
    let ObjectPrototype = Object.prototype,
      defineProperty = Object.defineProperty,
      prefix = `__simbol${Math.random()}__`,
      id = 0;

    function get() { /* avoid set w/out get prob */ }

    function SingletonSymbol() {
      const __symbol__ = prefix + id++;
      defineProperty(
        ObjectPrototype,
        this._ = __symbol__,
        {
          enumerable: false,
          configurable: false,
          get, // undefined
          set(value) {
            defineProperty(this, __symbol__, {
              enumerable: false,
              configurable: true,
              writable: true,
              value
            });
          }
        }
      );
    }

    defineProperty(SingletonSymbol.prototype, 'toString', {
      enumerable: false,
      configurable: false,
      writable: false,
      value: function toString() {
        return this._;
      }
    });

    return SingletonSymbol;
  }(Object));
}

export default SingletonSymbol;

singleton.js

/* eslint-disable */
const SingletonSymbol = require('./singletonSymbol');

export default class Singleton {
  static get instance() {
    if (!this[SingletonSymbol]) {
      this[SingletonSymbol] = new this();
    }

    return this[SingletonSymbol];
  }

  constructor() {
    const Class = new.target ? new.target : this.constructor;

    if (!Class[SingletonSymbol]) {
      Class[SingletonSymbol] = this;
    }

    return Class[SingletonSymbol];
  }
}

personManager.js

import Singleton from 'infrastructure/helpers/singleton';
class Person extends Singleton{
  name;
  constructor() {
    super()
    this.name = 'John';
    this._init();
  }
  _init = () => {
      this.name = 'Mario';
  }
  introduceSelf() {
    console.log(`Hi! I'm ${this.name}`);
  }

}

I will appreciate any answer that will help me to understand the idea and if all that singleton is something that is deprecated and can be deleted from my project

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文