首页 > 解决方案 > 为什么*不*我需要设置或读取类属性的方法?

问题描述

在将 getter 和 setter 放在一起时,我意识到它们不是必需的。
我错过了什么?如何确保当我调用属性时它们是类中的属性?

'use strict';

const { Book } = require('../app');
const { Page } = require('../page');
const { Bookmark } = require('../bookmark');
//
const chai = require('chai');
const expect = chai.expect;

it('has a bookmark', function () {
  let bookmark = new Bookmark('bookmark');
  bookmark.material = 'paper';
  bookmark.mm='a';
  expect(bookmark.mm).to.equal('a');
  expect(bookmark.material).to.equal('paper');
})  

#../bookmark.js
class Bookmark {
  constructor(name) {
    this.name = name;
  }
}

exports.Bookmark = Bookmark;

标签: javascriptclass

解决方案


推荐阅读