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

  • Home
  • SEARCH
  • 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 8257715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:17:29+00:00 2026-06-08T02:17:29+00:00

Given this code: function Test(){ var $D = Date.prototype; $D.addDays=function(value){this.setDate(this.getDate()+value);return this;}; $D.clone = function()

  • 0

Given this code:

function Test(){
    var $D = Date.prototype;
    $D.addDays=function(value){this.setDate(this.getDate()+value);return this;};
    $D.clone = function() {
        return new Date(this.getTime());
    };
    alert(new Date().addDays(5));
    var x = 5;
}

Test();

alert(new Date().addDays(1));
​alert(x);​

The only error is thrown by alert(x) which throws the “x is undefined” error, because x is limited to function Test()

The call to addDays() works both within the function and outside it.

I’m assuming this is because the scope of the Date prototype is global, so it’s irrelevant that I extend it within a closure.

So if I wanted to extend Date within a closure, BUT NOT allow any other javascript to get the extension, how would I do it?

The reason I’m asking is because when I’m extending core javascript objects, I don’t want to conflict with other libraries which may make the same modifications.

  • 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-08T02:17:31+00:00Added an answer on June 8, 2026 at 2:17 am

    There are a couple ways to do what you’re talking about. You can just delete your changes when you’re done:

    function Test() {
        var $D = Date.prototype;
        // ...
        delete $D.addDays;
        delete $D.clone;
    }
    

    This would work if you needed the methods only during a single execution. It wouldn’t work if you need the methods to be available indefinitely, but only within your module.

    If that’s not the case, you can add the extension methods directly to the Date objects you (not to the prototype). Within your module, you could have a function that creates these special Date objects for you:

    function createDate() {
        var d = new Date();
        d.method1 = function() { ... };
        d.method2 = function() { ... };
        return d;
    }
    

    Finally, you can use helper functions scoped within your closure instead of extending the prototype. Really, extending the prototype is just syntactic sugar for you in this case anyway.

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

Sidebar

Related Questions

Given the following jquery code.. jQuery.fn.sliding = function () { return this.each(function () {
Given this code: var arrayStrings = new string[1000]; Parallel.ForEach<string>(arrayStrings, someString => { DoSomething(someString); });
i have this code $test = new test(); $test->var_test = array('one','two'); class test{ var
i have this code: $.getJSON('newMessageDetails', function (json) { var old_id = document.getElementById(td_id).value; var messages_count
Given the following code: function Person(firstName, lastName) { this.FirstName = firstName; this.LastName = lastName;
Given this code: List<Integer> ints = new ArrayList<Integer>(); // Type mismatch: // cannot convert
Given this code, tableButton = new JButton(new ImageIcon(80F_FG2015.GIF)); how would I get that String
Given this code: List<string> things = new List<string>(); foreach (string thing in things) {
I have this code: function makeUser() { $info['cn'] = Test User; $info['sn'] = User;
So I was given this code in a folder of bits and pieces that

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.