Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Location
    USA
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question *Please look at these* Properties and Methods & Changing Properties

    I HAVE BEEN TRYING TO FIGURE OUT THE PARAMETERS IN THE FUNCTIONS, INCLUDING THE ARRAYS [i], [p], ["get_" + i], ["set_" + i] AND HOW THE METHODS ARE BEING CALLED IN RELATION TO i, p and val, so that I can make sense of this code. If possible can you explain the code also

    What I will like to know are the properties and methods of the objects generated in the following code?

    Also I want to know how I can use those methods to show and change the name and age for the emp2 object.


    this is the code:


    function Employee(properties) {
    for (var i in properties) {
    if (properties.hasOwnProperty(i)
    && typeof properties[i] != 'function')
    {
    this["get_"+i] = (function(p) {
    return function() { return properties[p]; };
    })(i);
    this["set_"+i] = (function(p) {
    return function(val) { properties[p] = val; };
    })(i);
    }
    }
    }

    var emp1 = new Employee({ name: "Bob", age: 35, foo: function() {} });
    var emp2 = new Employee({ name: "Gary", age: 54 });

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    This is a way to create generic object instances each of which has their own set of properties. It will automatically generate the getter and setter methods for those properties.

    So after
    Code:
    var emp1 = new Employee({ name: "Bob", age: 35, foo: function() {} });
    you can use
    Code:
    var currentAge = emp1.get_age();
    to get the age of the instance "emp1" and
    Code:
    emp1.set_age(55);
    to set the age of instance "emp1" to 55


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •