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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:46:25+00:00 2026-05-30T23:46:25+00:00

Inside John Resig’s book Pro Javascript techniques he describes a way of generating dynamic

  • 0

Inside John Resig’s book “Pro Javascript techniques” he describes a way of generating dynamic object methods with the below code:

// Create a new user object that accepts an object of properties
function User(properties) {
    // Iterate through the properties of the object, and make sure
    // that it's properly scoped (as discussed previously)
    for (var i in properties) {
        (function() {
            // Create a new getter for the property
            this["get" + i] = function() {
                return properties[i];
            };
            // Create a new setter for the property
            this["set" + i] = function(val) {
                properties[i] = val;
            };
        })();
    }
}

The problem is when I try instantiating the above object, the dynamic methods are being attached to the window object instead of the object instantiated. It seems like “this” is referring to the window.

// Create a new user object instance and pass in an object of
// properties to seed it with
var user = new User({
name: "Bob",
age: 44
});

alert( user.getname() );

Running the above code throws this error “user.getname is not a function”.

What is the correct way of generating the dynamic functions for each object instantiated?

  • 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-05-30T23:46:26+00:00Added an answer on May 30, 2026 at 11:46 pm

    Is this code from the book? I have the book, but I haven’t read through it.

    It’s an error in the book. Check the errata: http://www.apress.com/9781590597279

    Inside the anonymous function, this is the global window.

    You could make it work by adding .call(this, i) after it.

    function User(properties) {
        // Iterate through the properties of the object, and make sure
        // that it's properly scoped (as discussed previously)
        for (var i in properties) {
            (function(i) {
                // Create a new getter for the property
                this["get" + i] = function() {
                    return properties[i];
                };
                // Create a new setter for the property
                this["set" + i] = function(val) {
                    properties[i] = val;
                };
            }).call(this, i);
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

John Resig’s Simple Javascript Inheritance: http://ejohn.org/blog/simple-javascript-inheritance/ I tried to do such thing: var SomeClass
I'm using John Resig's excellent javascript class for simple javascript inheritance and I'm having
This is from John Resig`s Learning Advanced JavaScript #35 http://ejohn.org/apps/learn/#35 , called What happens
So, I'm still reading Apress Pro Javascript Techniques and i'm having troubles with closures.
This is taken from John Resig`s Learning Advanced Javascript #25, called changing the context
Inside a service, what is the best way to determine a special folder path
Inside my code I'm generating hashes of URLs, (which are practically of unbounded length).
inside of my Zend_Db_Table_Rowset Object i found this: [_primary:protected] ... does anybody if theres
Is there a simple way to make Django render {{myform.name}} as John Smith instead
I was wondering if there are any way to put break point inside an

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.