Last active
August 29, 2015 14:13
-
-
Save olivmonnier/ea2b3578b256159aec9e to your computer and use it in GitHub Desktop.
Create Accessor attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var accessors = [ | |
{ | |
name: "Titi", | |
default: "Hello World", | |
get: function(attr){ | |
return attr + ' Toto'; | |
}, | |
set: function(attr){ | |
attr = attr + ' Tata'; | |
} | |
}, | |
{ | |
name: "Chouchou", | |
get: function(attr){ | |
return attr + ' Bol'; | |
}, | |
set: function(attr){ | |
attr = attr + ' Balle'; | |
} | |
} | |
] | |
var create_attr = function(accessors){ | |
for(var i in accessors){ | |
var accessor = accessors[i]; | |
if(accessor.hasOwnProperty('name')){ | |
var nameAttribute = accessor.name; | |
var uppercaseName = nameAttribute.charAt(0).toUpperCase() + nameAttribute.slice(1); | |
if(accessor.hasOwnProperty('get')){ | |
accessor['get' + uppercaseName] = accessor.get; | |
delete accessor.get; | |
} | |
if(accessor.hasOwnProperty('set')){ | |
accessor['set' + uppercaseName] = accessor.set; | |
delete accessor.set; | |
} | |
} | |
console.log(accessor); | |
} | |
} | |
create_attr(accessors); | |
/** | |
/* TODO: if there is no methods get or set, create automatically these methods | |
* Get method: function(){ | |
return attr; | |
} | |
* Set method: function(val){ | |
attr = val; | |
} | |
* if default attribute, do attr = default | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment