简体   繁体   English

如何在JavaScript中扩展Object而不会丢失原始功能

[英]How to extend Object in JavaScript without losing the original functionality

I have a JavaScript object defined like so: 我有一个像这样定义的JavaScript对象:

var Object = (function () {
    function Object() {
        this.id = RandomNumber();
    }

    // Custom Object.prototype / Object impementations here...

    return Object;
})();

The problem is that once this has been constructed, it loses original functionality like Object.defineProperty etc. 问题是,一旦构建它,它就会失去像Object.defineProperty等原始功能。

The idea is that I want to extend the basic functionality of Object, not re-write or overwrite the existing prototype. 我的想法是,我想扩展Object的基本功能,而不是重写或覆盖现有的原型。

How can this be achieved? 怎么能实现这一目标?

EDIT: Just to be clear, I know I can do this without affecting the original functionality: 编辑:为了清楚,我知道我可以做到这一点而不影响原来的功能:

Object.prototype.foo = function() { }

but I need to specifically add functionality to Object's constructor, ie 但我需要专门为Object的构造函数添加功能,即

function Object() { this.id = 0; }

The new functionality must not overwrite the original Functionality. 新功能不得覆盖原始功能。

Use the .prototype to add a property: 使用.prototype添加属性:

Object.prototype.specialMethod = function () {
    // Your method's code
};

And you'd use it like: 你会像以下一样使用它:

var a = {};
a.specialMethod();

Although I would discourage adding a property to the Object 's prototype, because it is enumerable and will mess up looping, and will be inherited by all objects, and objects that inherit from Object , which is basically everything. 虽然我不鼓励在Object的原型中添加属性,因为它是可枚举的并且会使循环陷入混乱,并且将被所有对象和从Object继承的Object继承,这基本上就是一切。

You could actually use the Object.defineProperty method you mention: 您实际上可以使用您提到的Object.defineProperty方法:

Object.defineProperty(Object.prototype, "specialMethod", {
    enumerable: false,    // The important one, to avoid looping problems
    configurable: false,
    writable: false,
    value: function () {
        // Your method's code
    }
});

In order to extend this object you should create another object that has its prototype assigned a new instance of Object. 为了扩展此对象,您应该创建另一个对象,该对象的原型分配了一个新的Object实例。

var Object = (function () {
    function Object() {
        this.id = 5;
    }

    Object.prototype.speak = function(prop){
       alert(this[prop]);
    }

    return Object;
})();

function ExtendsObject(prop){
    this.someProperty = prop;
}

ExtendsObject.prototype = new Object();

var xObj = new ExtendsObject("derived");
xObj.speak("id");
xObj.speak("someProperty");

Working Example: http://jsfiddle.net/RbCcA/ 工作示例: http //jsfiddle.net/RbCcA/

If you want to stick with the self executing functions here is the example rewrote: 如果你想坚持使用自执行函数,那么重写的例子是:

var Object = (function () {
    function Object() {
        this.id = 5;
    }

    Object.prototype.speak = function(prop){
       alert(this[prop]);
    }

    return Object;
})();

var ExtendsObject = (function(){

    function ExtendsObject(prop){
       this.someProperty = prop;
    }

    ExtendsObject.prototype = new Object();

    return ExtendsObject;
})();

var xObj = new ExtendsObject("derived");
xObj.speak("id");
xObj.speak("someProperty");

Working Example: http://jsfiddle.net/RbCcA/1/ 工作示例: http //jsfiddle.net/RbCcA/1/

I do question the use of self executing functions in this situation. 我确实质疑在这种情况下使用自执行功能。 They are usually used to encapsulate and shield internals, however in the code example they are being exposed by returning the object from the SEF. 它们通常用于封装和屏蔽内部,但在代码示例中,它们通过从SEF返回对象来暴露。 Returning the object and storing it in a global variable just re-exposes the object, allowing its prototype and properties to be manipulated. 返回对象并将其存储在全局变量中只需重新公开对象,从而允许对其原型和属性进行操作。 Maybe there are private variables you have not mentioned, but as stated I find the SEFs unnecessary. 也许你没有提到私人变量,但正如所述,我发现SEF是不必要的。

Do as Ian wrote. 就像伊恩写的那样。 If you also want to check it the method already exists use 如果您还想检查它已经存在的方法使用

if (Object.prototype.specialMethod == null) Object.prototype.specialMethod = function() { ... };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Javascript中添加/替换嵌套对象中的值(不丢失原始引用)? - How to add / replace values in a nested object (without losing original references) in Javascript? 如何在不丢失原件的情况下更换方法? - How to replace a method without losing the original? 如何在不丢失 PostBack 功能的情况下将我自己的 JavaScript 链接到客户端 - How do I chain my own JavaScript into the client side without losing PostBack functionality 扩展没有jQuery的JavaScript对象 - Extend javascript object without jquery Javascript:“扩展”没有实例的对象 - Javascript : “extend” an object without an instance 如何在嵌套树对象中进行过滤而不丢失javascript中的结构? - How filter in a nested tree object without losing structure in javascript? 在不丢失代码功能的情况下将数组从 JavaScript 传递到 PHP - Passing array from JavaScript to PHP without losing code functionality 扩展功能javascript的功能 - Extend functionality of function javascript 如何在javascript中扩展(使用新名称)静态函数而不影响原始函数? - How do I extend (with a new name) a static function in javascript without affecting the original? JavaScript,覆盖对象而不会丢失引用 - JavaScript, overwrite object without losing reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM