15

如何为原型指定默认 getter?对于默认 getter,我的意思是调用 if 的函数obj.undefinedProperty123

我试过 Object.prototype.get = function(property) {..} 但在这种情况下不会调用它。

4

8 回答 8

12

在 ECMAScript 5 中,您只能通过以下方式拦截特定命名属性(并非所有属性)的 get/set 操作Object.defineProperty

Object.defineProperty(someObj, "someProp", {
    get: function() {
        console.log("you tried to get someObj.someProp");
        return "foo";
    }
});

在这里,该get函数将在代码尝试读取的任何时间运行someObj.someProp

在即将到来的 ECMAScript 6 草案中,这将通过代理实现。代理具有底层目标对象和设置/获取功能。set任何时候在任何代理的属性上发生 set 或 get 操作时,相应的函数都会运行,将代理的目标对象、使用的属性名称和尝试中使用的值作为参数。

var proxyHandler = {
    get: function(obj, name){
        console.log("you're getting property " + name);
        return target[name];
    },
    set: function(obj, name, value) {
        console.log("you're setting property " + name);
        target[name] = value;
    }
}

var underlyingObj = {};

// use prox instead of underlyingObj to use get/set interceptor functions
var prox = new Proxy(underlyingObj, proxyHandler);

在这里,设置为获取属性值prox将导致set/get函数运行。

于 2014-09-29T17:18:07.533 回答
4

You need to wait for the implementation of the ECMA6 "Proxy" system, designed to do exactly this. See http://wiki.ecmascript.org/doku.php?id=harmony:direct_proxies.

于 2012-12-18T06:32:56.460 回答
4

加雷斯所说的,除了它是__noSuchMethod__

或者,也许您正在考虑 PHP?

这是一篇关于最近标准化的属性 getter/setter 的非常好的文章,重点介绍了一些以前的非标准化身。

http://whereswalden.com/2010/04/16/more-spidermonkey-changes-ancient-esoteric-very-rarely-used-syntax-for-creating-getters-and-setters-is-being-removed/

摘要:目前还没有标准的“包罗万象”的 getter / setter,但 Object.defineProperty 是未来。

于 2010-07-05T06:29:50.757 回答
2

你想创建一个代理

const data = {};

const proxy = new Proxy(data, {
  get: (target, prop) => {
    console.log({ target, prop });
    return "My Value";
  },
  set: (target, prop, value) => {
    console.log({ target, prop, value });
    return true;
  },
});

proxy["foo"] = "bar";
const bar = proxy["foo"];
于 2020-09-02T13:31:43.800 回答
1

Firefox 可以使用非标准的 noSuchMethod:-

({__noSuchMethod__:function(){alert(1);}}).a();
于 2010-06-24T22:14:37.743 回答
0

我不确定你在问什么。但是,如果您希望在用户尝试访问时调用方法object.nonExistingProperty。我不认为有任何方法可以做到这一点。

于 2010-06-24T18:30:33.727 回答
0

也许迟到了,让我们添加简单的 ES5 友好“类创建”:字符串道具和吸气剂 - 我需要它来进行一些古老的重写,以暂时支持 IE(是的,这很痛,仍然发现有人依赖 ActiveX)

var MyObj = function () {
  var obj = {
    url: 'aabbcc',
    a: function(){ return this.url;}
  }  
  Object.defineProperty(obj, "urltoo", { get: function () { return this.url; } })
  return obj;
}

var X = new MyObj();
x.url // aabbcc
x.urltoo // aabbcc
x.urltoo() // error - not a function
x.a() // aabbcc
x.a // ƒ (){ return this.url;}
于 2021-07-02T09:31:23.520 回答
0

我遇到了这个问题,因为我想要这种行为:如果对象属性未定义,则返回一些默认值。

const options = {
  foo: 'foo',
  bar: 'bar',
  baz: 'baz'
};

function useOptionValue(optionName) {
  // I want `options` to return a default value if `optionName` does not exist
  const value = options[optionName];
  // etc...
}

执行此操作的简单方法(对于此用例没有 Proxy 或 Object.defineProperty 过度杀伤)如下所示:

function useOptionValue(optionName) {
  const value = options[optionName] || defaultValue;
}

或者,如果你想变得花哨,你可以使用Symbol

const default = Symbol.for('default');

const options = {
  foo: 'foo',
  bar: 'bar',
  baz: 'baz',
  [default]: 'foobarbaz'
};

function useOptionValue(optionName) {
  const value = options[optionName] || options[default];
}
于 2021-09-21T18:13:32.040 回答