Javascript 的内部实现

Internal Implementation of Javascript

最近开始学习JS,主要是想学习node及相关框架。 我已经开始从 freecodecamp 做一些基础教程并参考 Mozilla MDN

的文档

我从 ES6 构造函数开始,但坚持了一些东西。详情如下:

class Book {
  constructor(author) {
    this._author = author;
  }
  // getter
  get writer() {
    return this._author;
  }
  // setter
  set writer(updatedAuthor) {
    this._author = updatedAuthor;
  }
}

getter 和 setter 的调用很奇怪,来自 Java,但没关系。适应!!

const ALEPH = new Book("Paulo Coelho");
let writtenBy = ALEPH.writer;
ALEPH.writer = "So we changing Authors now !!"

以上代码(语法)在考虑时是有意义的:

const person = {
  name: "Taylor",
  sayHello() {
    return `Hello! My name is ${this.name}.`;
  }
};

而且 class 不是真正的 class 形式的 OOP 概念,而是一种称为构造函数的模板。

It should be noted that the class syntax is just syntax, and not a full-fledged class-based implementation of an object-oriented paradigm, unlike in languages such as Java, Python, Ruby, etc.

这是有道理的,getters 和 setters 是如何被调用的,因为它们只不过是对象的 keys/attributes 持有一个针对它的函数。

如果我的理解有误,或者我需要更专业的纠正,请指正

问题是,我在互联网上查看了 getset 关键字在内部的作用。 我找到了实现以及如何使用它们,而不是它们在内部做什么。

使用Java,我可以很容易地参考内部实现或者在关键字的情况下,查找Java规范或JVM规范来理解引擎盖下发生了什么。我正在为 JS 寻找类似的东西。最好能参考一下这样的官方文档。

问题:

  1. get 和 set 关键字在内部是如何实现的?
  2. JS 的任何 官方文档 就像我们为 Python 或 Java.

谢谢。

你真棒!

你已经注意到了关于 JS 的这些事情,这是真的,JS 类 不是真实的 classes,JS 有 OLOO(对象 linked 到其他对象)所以我们只有普通对象,没有任何真正的对象 class,这导致我们采用行为委托模式,它比 OOP 更简单,更适合 JS。

我认为可以在 EcmaScript 262 标准中找到内部结构 check it here

我还想提一提一本很棒的 JS 书籍系列,它带您深入了解名为 YDKJS 的内容,这里是 link 关于 getters and setters

不要因 ES6 功能(classextends 等...)而分心。 JavaScript 中没有 classclass based inheritance 之类的东西。它们只是原生 JavaScript 函数 上的 语法糖 functions 是 JavaScript 中的前 class 个对象。

例如,上面的 Book class 将在 ES5 中使用 babel repl

进行如下转换
"use strict";

function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } }

function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

var Book = /*#__PURE__*/function () {
  function Book(author) {
    _classCallCheck(this, Book);

    this._author = author;
  } // getter


  _createClass(Book, [{
    key: "writer",
    get: function get() {
      return this._author;
    } // setter
    ,
    set: function set(updatedAuthor) {
      this._author = updatedAuthor;
    }
  }]);

  return Book;
}();

查看 getset 如何在 ES5 中转换。

MDN docs 足以深入了解 JavaScript 及其功能。

结语:尽量避免比较JavaJavaScript。把它当作新东西来学习