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 9063129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:54:45+00:00 2026-06-16T15:54:45+00:00

I’m referring to this article . I’m basically creating a class with an init

  • 0

I’m referring to this article.
I’m basically creating a class with an init function which set an options attribute. see http://jsbin.com/usaboh/1/edit for the working example (enable the console to see the output).

Below the first example works fine, however the second still get the options value of the first “instance”.

Works as expected :

var Person = Class.extend({
    options: {value: 2},
    init: function(options){
        if(options) {
            this.options = options;
        }
    }
});

var p = new Person({value: 5});
console.log(p.options); //{"value": 5} (ok)

var p2 = new Person();
console.log(p2.options); //{"value": 2} (ok)

Doesn’t work as expected :

var Person = Class.extend({
    options: {value: 2},
    init: function(options){
        if(options) {
            for (var name in options) {
                this.options[name] = options[name];
            }
        }
    }
});

var p = new Person({value: 5});
console.log(p.options); //{"value": 5} (ok)

var p2 = new Person();
console.log(p2.options); //{"value": 5} (why 5 ??? shouldn't be 2 ?)

Why the value of options in the second instance is not {"value": 2} like in the first example ?

  • 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-16T15:54:46+00:00Added an answer on June 16, 2026 at 3:54 pm

    That’s because options is a property of the class (on its prototype), not the object.

    Verify it by adding this to the bottom of your second example:

    console.log( p.options == p2.options ); // true
    

    Explanation:

    When using abstraction layers, it is very important to remember the underlying principles, so that you don’t get bitten by the abstractions’ leaks.

    Your 1st example:

    Let’s re-write your class definition with regular Prototypal inheritance:

    var Person = function (options) {
        if (options) {
            this.options = options;
        }
    };
    
    Person.prototype.options = {value: 2};
    
    var john = new Person();
    

    john will now be a new instance of Person and will not have an options property:

    john.hasOwnProperty('options'); // false
    

    When trying to access the options via john.options, you’ll get the {value: 2} from the prototype chain.

    If however you were to create this new Person object by supplying your options to the constructor:

    var michael = new Person({value:5});
    

    michael will have an options property:

    michael.hasOwnProperty('options'); // true
    

    Here’s a simple test comparing the two:

    john.options == Person.prototype.options // true
    michael.options == Person.prototype.options // false
    

    Your 2nd example:

    Now consider your second example, written in native Prototypal inheritance:

    var Person = function (options) {
        if (options) {
            for (var name in options) {
                this.options[name] = options[name];
            }
        }
    };
    
    Person.prototype.options = {value: 2};
    

    Regardless of whether you pass options into the constructor, your new object will never have its own options property. All you’re doing is augmenting the options on the prototype:

    var john = new Person();
    john.options // {value:2}
    john.hasOwnProperty('options'); // false
    john.options == Person.prototype.options // true
    
    var michael = new Person({value:5});
    michael.options // {value:5}
    michael.hasOwnProperty('options'); // false
    michael.options == Person.prototype.options // true
    
    john.options // {value:5}
    // Why? Because they share the same `options` property from the prototype
    john.options == michael.options // true
    

    If you then go and assign a new object to john, it won’t affect the options on the prototype:

    john.options = {value:10};
    john.options // {value:10}
    john.options == Person.prototype.options // false
    michael.options // {value:5}
    michael.options == john.options // false
    michael.options == Person.prototype.options // true
    

    Solution:

    To get the functionality you want in your second example, you’ll have to follow these steps:

    1. Create a new options property from within the constructor (the init method).
    2. Copy all the properties from Person.prototype.options.
    3. Finally, copy all the properties from the options argument.

    Here’s how you might do that:

    var Person = Class.extend({
        options: {value: 2},
        init: function(options) {
            if (options) {
                var name, prototypeOptions = Person.prototype.options;
                this.options = {};
    
                for (name in prototypeOptions) {
                    this.options[name] = prototypeOptions[name];
                }
    
                for (name in options) {
                    this.options[name] = options[name];
                }
            }
        }
    });
    

    If you’re using jQuery or underscore, you can use their extend method, which makes all this trivial:

    var Person = Class.extend({
        options: {value: 2},
        init: function(options) {
            if (options) {
                this.options = $.extend({}, Person.prototype.options, options);
            }
        }
    });
    

    Simpler solution:

    Depending on your needs, you might not even need the options to be on the prototype in the first place. Just add it to your object in the constructor (the init method), and be done with it:

    var Person = Class.extend({
        init: function(options) {
            this.options = {value: 2};
            if (options) {
                for (var name in options) {
                    this.options[name] = options[name];
                }
            }
        }
    });
    

    and once again, if you’re using jQuery or underscore, this becomes even simpler:

    var Person = Class.extend({
        init: function(options) {
            this.options = _.extend({value: 2}, options);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am doing a simple coin flipping experiment for class that involves flipping a

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.