Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding a function without removing static properties

If I have a function like this:

function a() {
    console.log('a');
}

and then assign a static property like this:

a.static = 'foo';

But say I want to override the function with another function like this:

var old = a;

a = function() {
    console.log('new');
    old.call(this);
};

a.static // undefined

Since I assigned a new function to a, it’s static properties are lost. Is there a neat way to keep the static properties without looping and manually copying them?

Update:

Here’s a real world scenario: In Bootstrap jQuery plugins, the author assigns defaults to the property function like this:

$.fn.modal = function() {
    // some code
};

$.fn.modal.defaults = { // some object };

So if I want to "extend" the prototype I would normally do:

var old = $.fn.modal;

$.fn.modal = function() {
    // do my thing
    old.apply(this, arguments);
}

But that would make

$.fn.modal.defaults === undefined

This will break the functionality, because the defaults are lost. I was wondering if there a sneaky way in javascript to change only the function without losing the static properties.

like image 365
David Hellsing Avatar asked Oct 02 '12 07:10

David Hellsing


People also ask

Can you override a static function?

No, we cannot override static methods because method overriding is based on dynamic binding at runtime and the static methods are bonded using static binding at compile time. So, we cannot override static methods. The calling of method depends upon the type of object that calls the static method.

Can overriding method be static and private?

You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding. Similarly, you cannot override a private method in sub class because it's not accessible there.

Can you declare an overridden method to be static if the original method is not static in C#?

No, they can't be overridden. They are associated with the class, not with an object. for the real use: you can call a static method without the class instance.

Can we override a static method What is method hiding?

Static methods cannot be overridden because method overriding is predicated on dynamic binding at runtime, whereas static methods are bonded via static binding at compile time. As a result, we cannot override static methods.


1 Answers

No, you cannot do this. Replacing the object (function) always takes any properties with it.

There are two solutions here, and both involve transferring the properties from the old object to the new one.

The first (recommended) approach is to copy the properties, which can be done conveniently with $.extend:

$.fn.plugin = $.extend(function() { ... }, $.fn.plugin);

The second option would be to dynamically set the prototype of the new function to be the old function. For example, in some browsers this would work:

var f = function() { ... };
f.__proto__ = $.fn.plugin;
$.fn.plugin = f;

However this is non-standard and might give rise to complications; don't do it.

like image 75
Jon Avatar answered Oct 23 '22 20:10

Jon