是Object.defineProperty还是.prototype? [英] Object.defineProperty or .prototype?

查看:61
本文介绍了是Object.defineProperty还是.prototype?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了两种不同的在javascript中实现非本机功能的技术,首先是:

I've seen two different techniques of implementing non-native features in javascript, First is:

if (!String.prototype.startsWith) {
    Object.defineProperty(String.prototype, 'startsWith', {
        enumerable: false,
        configurable: false,
        writable: false,
        value: function(searchString, position) {
            position = position || 0;
            return this.lastIndexOf(searchString, position) === position;
        }
    });
}

第二个是:

String.prototype.startsWith = function(searchString, position) {
    position = position || 0;
    return this.lastIndexOf(searchString, position) === position;
}

我知道第二个用于将任何方法附加到特定标准内置对象的原型链上,但是第一个技术对我来说是新的.任何人都可以解释它们之间的区别,为什么使用一个,为什么不使用它们以及它们的意义是什么.

I know that the second one is used to attach any method to the prototype chain of a particular standard built-in objects, but first technique is new to me. Can anybody explain what is the difference between them, why one is used and why one not and what are their significances.

推荐答案

在两种情况下,您要在 String.prototype 中添加新属性'startsWith'.

In two cases you are adding a new property 'startsWith' in String.prototype.

在这种情况下,第一个不同于第二个:

First differs from the second in this cases:

您可以将属性配置为可枚举可写可配置.

You can configure the property to be enumerable, writable and configurable.

可写 - true 表示您可以通过分配任何值来更改其值.如果为假-您将无法更改该值

Writable - true means that you can change its value by assigning any value. If false - you can't change the value

Object.defineProperty(String.prototype, 'startsWith', {
        enumerable: false,
        configurable: false,
        writable: false, // Set to False
        value: function(searchString, position) {
            position = position || 0;
            return this.lastIndexOf(searchString, position) === position;
        }
    });

var test = new String('Test');

test.startsWith = 'New Value';
console.log(test.startsWith); // It still have the previous value in non strict mode

可枚举 - true 意味着它将在 for in 循环中看到.

Enumerable - true means that it will be seen in the for in loop.

Object.defineProperty(String.prototype, 'startsWith', {
        enumerable: true, // Set to True
        configurable: false,
        writable: false, 
        value: function(searchString, position) {
            position = position || 0;
            return this.lastIndexOf(searchString, position) === position;
        }
    });

var test = new String('Test');

for(var key in test){
   console.log(key)  ;
}

可配置 - true ,并且仅当此属性描述符的类型可以更改并且该属性可以从相应的对象中删除时.

Configurable - true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object.

Object.defineProperty(String.prototype, 'startsWith', {
            enumerable: false,
            configurable: false, // Set to False
            writable: false, 
            value: function(searchString, position) {
                position = position || 0;
                return this.lastIndexOf(searchString, position) === position;
            }
        });

    
    delete String.prototype.startsWith; // It will not delete the property
    console.log(String.prototype.startsWith);

还有一个建议,不要更改构建类型的原型.

这篇关于是Object.defineProperty还是.prototype?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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