object - Javascript 'property inheritance' -
Is there any way to specify the common element for object literals in an array? For example: <"Hello", Formatter: deleteCheckboxFormatter}, {{Key: "Hello", label: "hi", formatter: deleteCheckboxFormatter}, {key: "hello", label: key : "Wut", label: "what?", Formatter: deleteCheckboxFormatter}];
All three records use the same formatter How would you refactor it?
A couple of options come to my mind:
For the general field An assistant function with the default value:
function (key, label) {return {'key': key, 'label': label, format: deleteCheckboxFormatter}; } Var array = [create ("hi", "hi"), ("hello", "hello"), ("knit", "what?")];
or more generic function that accepts an argument for the formatting property:
create the form (formatter) { Return function (key, label) {return {'key': key, 'label': label, 'formatter': formatter}; }} // is a function to create an object that contains 'fooFormatter' var foo = make ('fooFormatter'); Var array = [EFU ("Hi", "Hi"), EFU ("Hello", "Hello"), EFU ("woot", "what?")];
and the last thing in my mind is to repeat the common field with the specified array:
var array = [{key: "hi" , Label: "hi"}, {key: "hello", label: "hello"}, {key: "wut", label: "what?"}]; Var i = array.length; While (i--) {array [i] .formatter = 'deleteCheckboxFormatter'; }
I have used a loop for a while in the reverse order, because the order of repetition is not important and such loop
Comments
Post a Comment