在JavaScript中为属性添加别名 [英] Add an alias for a property in JavaScript

查看:147
本文介绍了在JavaScript中为属性添加别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这很简单,

是否有一种简单的方法为属性添加辅助名称(我认为这是特定于字符串的 - 我是不确定),即

Is there a simple way to add a secondary name for a property (I think this one is String specific – I'm not sure), i.e.,

c = length // this line pseudo code

'hello world'.length // returns 11
'hello world'.c      // this line is pseudo code, meant to return 11

在上面的示例中,为属性长度创建了一个别名。这可以用JavaScript吗?

In the example above, there's an alias created for the property length. Is that possible to do in JavaScript?

推荐答案

1。使用括号表示法

使用括号表示法,您可以像这样访问该物业:

With bracket notation, you can access the property like so:

'hello world'[c]

这与相同hello world'.length 如果 c 'length'作为字符串。

This does the same thing as 'hello world'.length if c is 'length' as a string.

var c = 'length';
console.log('hello world'[c]);

唯一的区别是该属性是一个字符串。括号表示法是属性访问者。

The only difference is that the property is a string. Bracket notation is a property accessor.

2。使用 Object.defineProperty()

2. With Object.defineProperty()

现在,如果你想要一个别名:

Now if you want an alias:

Object.defineProperty(String.prototype, 'c', {
    get: function() {
        return this.length;
    }
});

console.log("hello world".c);

以上使用 Object.defineProperty 定义现有对象的属性,String的 prototype 对象。这样,字符串的所有实例都将具有此新属性。根据文档:

The above uses Object.defineProperty to define a property for the existing object, String's prototype object. That way, all instances of a string will have this new property. Per the documentation:


Object.defineProperty()方法定义一个新属性直接在对象上,或修改对象上的现有属性,并返回该对象。

The Object.defineProperty() method defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

语法

Object.defineProperty(obj,prop,descriptor)

obj 是要修改的对象, prop 是新属性或现有属性, descriptor 是新属性或现有属性的描述符。

Where obj is the object being modified, prop is the new or existing property, and descriptor is the descriptor for the new or existing property.

因此,上面定义了对象的属性 String.prototype ,名称 c 。它的描述符是一个get函数,它返回这个的长度。在上面的示例中,引用字符串,因此它返回字符串的长度。您可以在此处了解有关获取者的更多信息。

Thus, the above defines a property for the object String.prototype, with name c. Its descriptor is a get function which returns the length of this. In the example above, this refers to the string so it returns the length of the string. You can read more about getters here.

通过更改为适用的原型( obj ),例如使用 Object.prototype 。但是,这有潜在的问题,因为尝试在没有长度属性的对象上返回 this.length 将返回undefined,如图所示这里。您还可以使用 对象.defineProperties 一次定义多个属性。

This can also be defined for more types by changing to the applicable prototype (obj), such as using Object.prototype instead. However, this has potential issues, as trying to return this.length on an object without a length property will return undefined, as seen here. You can also use Object.defineProperties to define multiple properties at a time.

这篇关于在JavaScript中为属性添加别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆