...

View Full Version : *Please look at these* Properties and Methods & Changing Properties



dcskye
01-16-2012, 01:02 PM
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 });

devnull69
01-16-2012, 03:16 PM
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


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

you can use


var currentAge = emp1.get_age();

to get the age of the instance "emp1" and


emp1.set_age(55);

to set the age of instance "emp1" to 55



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum