如何从javascript中的字符串调用私有函数?

时间:2012-04-30 19:51:51

标签: javascript oop

我正在构建一个小的“Validate”对象,该对象基本上公开了一个validate方法,并获取一个元素的id和一个验证器数组,然后返回true或false。

基本上这就是我想要实现的目标

var Validator = function() {
  var no_digits = function( el ) {
      return true;
  }
  var no_uppercase_letters = function( el ) {
      return true;
  }
  return {
     validate: function( element_id, validators ) {

        //here i would like to iterate on the validators array and for each 
        //element of the array i would like to check if a function of the same name 
        // exist and call that function passing the element      

     }
  }
}();

然后像这样调用它

var element_valid = Validator.validate( 'myid', [ "no_digits", "no_uppercase_letters"] );

其中第二个参数是我想要调用的验证器数组。

关于良好的面向对象方法的任何建议?我想保持验证功能私有,否则我可以做

var Validator = function() {


    return {
        validate: function(element_id, validators) {
            console.log(this);
            this[validators]();

            // Validator[validators](element_id);     
        },
        no_digits: function(el) {
            alert('hi');
            return true;
        },
        no_uppercase_letters:  function(el) {
            return true;
        }
    }
}();

但我宁愿将no_gits与no_uppercase_letters函数保持为私有

var element_valid = Validator.validate('myid',“no_digits”);

2 个答案:

答案 0 :(得分:3)

var valdiate = (function() {  

     var _p={};//toss all private members into a single object.
     _p.list=[];
     _p.init=function(){
       _p.list=[];
     };

    var noDigits = function() {

    };

    //public members. use "this" to reference object followed by the method.
    //however valdiate._p.list won't be accessible to the global scope    
    return {    
        check: function() {
              noDigits();
        },
        fnNaMe1:function(){
              _p.init();
        },
        fnName2:function(){
           return _p.list.slice(0);//return a clone  
        }
    };
})();

它被称为“模块模式”。这种模式在JavaScript中更常被称为“封装”。闭包是另一种可能,但更具体地说,它在这种情况下纯粹是封装。

封装只是意味着你让一些成员变得私密。在这种情况下私人是什么?那么noDigits变量在这种情况下是私有的。

答案 1 :(得分:1)

这种方法有用吗? jsFiddle这里:http://jsfiddle.net/FranWahl/KZKwA/可以玩 请注意,我没有实现任何实际验证,但基本框架确实执行。

var Validator = function() {
    return {
        validate: function(element_id, validators) {
            for (var i = 0; i < validators.length; i++) {
                var result = validators[i](element_id);
                alert(result);
                // record results... or do something else with it or break; etc...
            }
        }
    }
}();

var no_digits = function(theValue) {
    // validate no digits are in the given value....
    if(theValue === '1')
    {
        return true;
    }
    else
    {
        return false;
    }
};

var no_uppercase_letters = function(theValue) {
    // validate no uppercase letters are in this value...
    if(theValue === '4')
    {
        return true;
    }
    else
    {
        return false;
    }
};

// I used variables to store the methods but feel free to declare the methods inline instead...
var element_valid = Validator.validate('4', [no_digits , no_uppercase_letters]);​