确定 JavaScript 属性是否定义了 getter 或 setter?

发布于 2024-12-22 05:36:02 字数 142 浏览 1 评论 0原文

给定一个对象和属性名称,是否可以确定该属性是否是使用 getter 或 setter 定义的,还是完全透明的?如果属性上尚未定义 getter/setter,我只想定义一个 getter/setter。

我需要它在 WebKit/Firefox 中工作。

Is it possible, given an object and property name to determine if that property is defined using either a getter or setter, or is it completely transparent? I only want to define a getter/setter if there is not already one defined on the property.

I need it to work in WebKit/Firefox.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

素食主义者 2024-12-29 05:36:02

我认为您正在寻找 getOwnPropertyDescriptor

I think you're looking for getOwnPropertyDescriptor?

面如桃花 2024-12-29 05:36:02

您可以使用 Object.getOwnPropertyDescriptor(obj, prop)

例如:

var obj = { first: 1 }
obj.__defineGetter__('second', function() { return 2; });

// get descriptors
var descriptor1 = Object.getOwnPropertyDescriptor(obj, 'first');
var descriptor2 = Object.getOwnPropertyDescriptor(obj, 'second');

// check if it's a getter

descriptor2.get
// returns function () { return 2; }

descriptor1.get
// returns undefined

You can use Object.getOwnPropertyDescriptor(obj, prop)

For example:

var obj = { first: 1 }
obj.__defineGetter__('second', function() { return 2; });

// get descriptors
var descriptor1 = Object.getOwnPropertyDescriptor(obj, 'first');
var descriptor2 = Object.getOwnPropertyDescriptor(obj, 'second');

// check if it's a getter

descriptor2.get
// returns function () { return 2; }

descriptor1.get
// returns undefined
樱桃奶球 2024-12-29 05:36:02

您可以使用 Object.getOwnPropertyDescriptor(obj, prop)

function isGetter (obj, prop) {
  return !!Object.getOwnPropertyDescriptor(obj, prop)['get']
}

用法:

obj = {
  foo: 'foo',
  get bar() { return 'bar' }
}

isGetter(obj, 'bar') // will return true
isGetter(obj, 'foo') // will return false

You can use Object.getOwnPropertyDescriptor(obj, prop).

function isGetter (obj, prop) {
  return !!Object.getOwnPropertyDescriptor(obj, prop)['get']
}

Usage:

obj = {
  foo: 'foo',
  get bar() { return 'bar' }
}

isGetter(obj, 'bar') // will return true
isGetter(obj, 'foo') // will return false
渡你暖光 2024-12-29 05:36:02

Object.defineProperty 的 getset 方法如果未在描述符中定义,默认值将是未定义的。这是理想的识别方式。

让我们举个例子:

var User = {};

Object.defineProperty(User, 'name', {
    get: function () {
        return _name;
    },
    enumerable: true
});

Object.defineProperty(User, 'designation', {
    get: function () {
        return _designation;
    },
    set: function (val) {
        _designation = val;
    },
    enumerable: true
});


var d = Object.getOwnPropertyDescriptor(User, 'name');
console.log(d.set == undefined);

参考: https://developer .mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

Object.defineProperty's get or set Method default value will be undefined if its not defined in the descriptor. This is ideal way to identify.

Let's say example:

var User = {};

Object.defineProperty(User, 'name', {
    get: function () {
        return _name;
    },
    enumerable: true
});

Object.defineProperty(User, 'designation', {
    get: function () {
        return _designation;
    },
    set: function (val) {
        _designation = val;
    },
    enumerable: true
});


var d = Object.getOwnPropertyDescriptor(User, 'name');
console.log(d.set == undefined);

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

摇划花蜜的午后 2024-12-29 05:36:02

找到满足您的 java 脚本要求的示例对象模板:

var Man=function(n,a){
  var name,age;

  function getName(){
       return this.name;
  }

  function getAge(){
       return this.age;
  }
  function create(){
       this.name=n;
       this.age=a;
       console.log('new object created');
  }

  function setAge(age){
       this.age=age;
  }

  function setName(name){
      this.name=name;
  }
  return{
   name:n,
   age:a,
   getName:getName,
   getAge:getAge,
   setName:setName,
   setAge:setAge
  };
}

var m1=new Man('Chaiguy',30);

// To check if Property exist for an Object
console.log(m1.hasOwnProperty('name'));

// To get the name and age of the object

console.log(m1.getName()+' is '+m1.getAge()+' years old.');

Find the sample Object template for your java Script requirement:

var Man=function(n,a){
  var name,age;

  function getName(){
       return this.name;
  }

  function getAge(){
       return this.age;
  }
  function create(){
       this.name=n;
       this.age=a;
       console.log('new object created');
  }

  function setAge(age){
       this.age=age;
  }

  function setName(name){
      this.name=name;
  }
  return{
   name:n,
   age:a,
   getName:getName,
   getAge:getAge,
   setName:setName,
   setAge:setAge
  };
}

var m1=new Man('Chaiguy',30);

// To check if Property exist for an Object
console.log(m1.hasOwnProperty('name'));

// To get the name and age of the object

console.log(m1.getName()+' is '+m1.getAge()+' years old.');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文