- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望我的对象有一个字段,当读取时返回字段值,并且当将 val 写入该字段时,我想在写入之前修改 val。我当前的代码是这样的:
function Cat(lives) {
var self = this;
var privateLives;
Object.defineProperty(self, 'publicLives', {
get: function() {return privateLives;},
set: function(val) {privateLives = 7 * val;}
});
}
有没有办法在不创建私有(private)变量的情况下做到这一点?理想情况下,我只需让 setter 是这样的:
function(val) {self.publicLives = 7 * val;}
但是当 setter 调用自身时,这会导致溢出。是否有某种方法可以使其不循环 setter (因此只有 setter 范围之外的赋值才会调用 setter ,而 setter 中的赋值只会执行正常分配)?如果可能的话,当 setter 写入公共(public)字段时,我也不需要显式定义 getter。
最佳答案
不,这是不可能的 - 属性只能是数据属性或访问器属性,不能同时是两者。当然,您不一定需要将值存储在 setter 的私有(private)变量中,您也可以使用不同的属性或不同对象上的属性(例如在 @Oriol 的代理中)。如果您想避免私有(private)变量,“私有(private)”属性是标准方法:
function Cat(lives) {
this.publicLives = lives;
}
Object.defineProperty(Cat.prototype, 'publicLives', {
get: function() {return this._privateLives;},
set: function(val) { this._privateLives = 7 * val;}
});
但是你也可以做一些棘手的事情,通过使用重复重新定义的常量 getter 函数来隐藏“私有(private)变量”:
Object.defineProperty(Cat.prototype, 'publicLives', {
set: function setter(val) {
val *= 7;
Object.defineProperty(this, 'publicLives', {
get: function() { return val; }
set: setter,
configurable: true
});
}
});
关于javascript - Object.defineProperty只能修改setter吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31566400/
我正在寻找这两种方法之间的主要区别。 一些网站提到了可读性问题,但我的担忧主要是与性能相关。看起来像defineProperty()更快,但我找不到原因。 var FOR_TIME = 10000;
我正在做一个 JavaScript 项目,只是想知道为什么对象实例不继承 defineProperty() 和其他方法,而不必调用父类(super class)(superobject?) 对象方法。
在 React 配置中使用 eslint 在使用 Object.defineProperty 时出现错误。错误说: Avoid using Object.defineProperty, instead
Object.defineProperty函数会直接在一个对象上定义一个新的属性,或者修改一个对象的现有属性,并返回此对象。 一、简单使用 const obj = {} Object.defineP
我使用React、Webpack、Babel构建项目,希望它在IE8中运行,存在IE8不支持Object.defineProperty的问题。 我没有使用这个功能,但是 npm 包可以做到这一点,比如
我想构建一个检测对象更改的代理: 定义了新属性。 现有属性已更改。 代码示例 1 - defineProperty const me = { name: "Matt" } const proxy
下面我尝试使用defineProperties函数定义对象的属性,但是当我打印此脚本中的最后一行时,我得到了意外的结果。我希望在控制台上记录 2005 年,但我不断收到 2004 年。这同样适用于其他
假设在我的模块中我有这样的东西: Object.defineProperty(Array.prototype, 'sayHello', {get: functio
我最近为 node.js 创建了自己的模块,用于 koa 模块。它是一个类似 koa-i18n 的翻译模块。我研究了其他 koa 模块以了解函数/属性如何应用于 koa 上下文/请求,其中一些使用 O
这个问题已经有答案了: JavaScript closure inside loops – simple practical example (45 个回答) 已关闭 6 年前。 在我的代码中我有一个
我正在尝试使用 defineProperty 使属性不出现在 for...in 循环中,但它不起作用。这个代码正确吗? function Item() { this.enumerable =
我正在尝试从 ABAP 执行 JS 代码,但出现以下错误: Object.defineProperty is not a function. 我的代码: Object.defineProperty(o
所以我一直在我的主要 javascript 启动文件中使用一堆这些,我需要其中的 20-30 个。有没有办法可以从不同的文件中导出这些文件,以便清理我的主文件? Reflect.definePrope
询问 Object.defineProperty 如下所示: function testComponent(){ var testProperty; Object.defineProp
我最近重组了我的 Three.js 项目,我开始遇到一个问题,每个对象似乎都是用完全相同的几何形状和 Material 渲染的。我在调试器中跟踪了这个构造函数的问题: function Geometr
var funcSetter = { defineProperty: function(target, prop, descriptor) { if (prop) {
我是 js 世界的新手,发现 jquery 将许多属性声明为方法,这让我非常不舒服。例如 $("#foo").parent() 我认为应该是属性。 我知道js也可以定义属性,所以我想尝试将这些方法重新
前言: Feel free to skip to the actual question below, if you find the'backstory' here unnecessary. But
我正在尝试创建自己的类对象并使用它来存储应用程序的各种数据类型,这在使用已发布属性时一切正常,我可以将它们流式传输到磁盘并毫无问题地返回。但我还需要流式传输一些动态整数类型数组。
我试图覆盖我应用程序中所有 iframe 的 src 属性,因此无论 HTML 的值是什么,它们的 src 属性总是设置为“redirect.html”标签为其定义。到目前为止,我提出了以下建议,但它
我是一名优秀的程序员,十分优秀!