Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8610777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:12:29+00:00 2026-06-12T04:12:29+00:00

If I have a function like this: function a() { console.log(‘a’); } and then

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:12:30+00:00Added an answer on June 12, 2026 at 4:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a function like this: function foo(_this) { console.log(_this); } function bar()
I have somethign like this: <div id=container> <img id=screenFirstImg> </div> And js: $('a.screenThumbLink').click(function(){ console.log(
I have a json string like this: json = {'run': 'function() { console.log('running...'); }'}
I have some code like this: $('.play').on( 'click', function(){ console.log('click'); }); The .play element
I have a function like this void doSmth(Long... paramg){ } But I can't pass
Suppose I have a function like this (I use akka 2.0.2): def foo(message: String):
If I have a function like this: function abc($a,$b,$c = 'foo',$d = 'bar') {
I have an array like this (output from console.log): [122.42.123.1, 122.42.123.1, 122.42.123.1, awdawd, awdawd]
Say I have some code like this function Chart(start, end, controller, method, chart) {
I have the following div: <div id=foo data-callback=function(){console.log(1)}></div> I want to be able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.