开发者

Add existing element to Object

开发者 https://www.devze.com 2023-03-29 01:15 出处:网络
I have next object:开发者_运维问答 var myArray = new Object(); myArray[\"firstname\"] = \"Gareth\";

I have next object:

开发者_运维问答
    var myArray = new Object();

    myArray["firstname"] = "Gareth";
    myArray["lastname"] = "Simpson";

Also I have function that returns elements (it's value depends from different conditions):

function getNewElement(condition1, ..., conditionN) {

        ...
        return { "my btn": function() { alert(kkk); } }
}

How I can add to myArray value that returns me getNewElement function?


myArray["fn"] = getNewElement;


Do you mean

myArray["newElement"] = getNewElement();

or

myArray["my btn"] = getNewElement()["my btn"];

?


Another way to create new Objects

    var myObj = {
            firstName : "Gareth"
            lastName  : "Simpson"
            };

function getNewElement(condition1, ..., conditionN) {
  return { "my btn": function() { alert(kkk); } } 
} 

myObj.getNewElement;

//to call the function
myObj.getNewElement();
0

精彩评论

暂无评论...
验证码 换一张
取 消