简体   繁体   English

向对象添加属性

[英]adding properties to an object

I wrote this code and it`s simple , i have an empty object which will contain some other objects as properties , but the object stays empty and don`t add the needed properties .. 我写了这段代码,很简单,我有一个空对象,其中将包含其他一些对象作为属性,但是该对象保持为空并且不添加所需的属性。

let buildProfileClientValidator = function(form , rules){

    let elements = {};

    function init(){
       //Some code that works fine
       addElement(elementName , elementType);
       addElement(elementName , elementType);

        //the elements object should now have some other objects as properties  
        //but it`s empty !!!!
        console.log(elements);
     }

     function addElement(elementName , elementType){
        //this condition works fine 
        if( !elementExist(elementName) ){
        //console.log(elementName , elementType); also works fine -> the values of  elementName , elementType are present
        elements[elementName] = {
            type  : elementType,
            value : '',
            rules : (rules[elementName] == undefined) ? '' :  rules[elementName].split('|')
         };
       }
     }
 }

so what i`m missing ?! 所以我想念什么?

After examining your code, but not knowing how buildProfileClientValidator gets called (and specifically what the rules parameter is), I can trace the problem to this line: 在检查了您的代码之后,但不知道如何调用buildProfileClientValidator (特别是rules参数是什么),我可以将问题跟踪到此行:

 (rules[elementName] === undefined) ? '' :  rules[elementName].split('|')

I have modified your code (to make it testable) in the following fiddle ( https://jsfiddle.net/hssbsL19/40/ ) and when I replace that line with a static value, the code works. 我在下面的小提琴( https://jsfiddle.net/hssbsL19/40/ )中修改了您的代码(以使其可测试),当我用静态值替换该行时,代码起作用了。

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

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