简体   繁体   English

JavaScript中的属性设置问题

[英]properties setting issue in javascript

When I am sending the properties as name, value the name is sent as "name" instead of the argument value of name. 当我将属性作为名称发送时,值的名称将作为“名称”发送,而不是名称的参数值。 Is there a fix for it? 有解决办法吗? Right now I use case to send in right way. 现在,我使用案件以正确的方式发送。

Working one 工作一

/**
     * Change properties of the elements
     */
    this.changeProperties = function(type,value) {
        switch(type)
        {
        case "stroke":
            $.DrawEngine.changeProperties({"stroke":value});
            break;
        case "font-family":
            $.DrawEngine.changeProperties({"font-family":value});
            break;
        }
    };

Not working one 不工作一个

    this.changeProperties = function(type,value) {
            $.DrawEngine.changeProperties({"stroke":value});
}

Reason 原因

it sends {type:"red"} instead of {"stroke": "red"} 它发送{type:"red"}而不是{"stroke": "red"}

You probably need to pass it as 您可能需要通过

this.changeProperties = function(type,value) {
      var obj = {};
      obj[type] = value;
      $.DrawEngine.changeProperties(obj);
}

if you had been trying to add the object as {type:value} . 如果您试图将对象添加为{type:value} key is not evaluated as type's actual value instead it becomes the key itself. 键不会被评估为类型的实际值,而是会变成键本身。 So you would need to use array notation to insert the value of type being created as the key. 因此,您将需要使用数组符号插入要创建的类型的值作为键。

See this 看到这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM