揭示模式可用于将私有方法暴露为公共方法。当为了对象的运转而将所有功能放置在一个对象中以及想尽可能地保护该对象,这种揭示模式就非常有用。

板栗:

var myarray;
(function(){
  var astr = "[object Array]",
  toString = Object.prototype.toString;
  function isArray(a){
    return toString.call(a) === astr;
  }
  function indexOf(haystack,needle){
    var i=0,
    max = haystack.length;
    for( ; i<max; i++ ){
      if( haystack[i] === needle ){
      return i;
    }
  }
  return -1;
}
myarray = {
  isArray : isArray,
  indexOf : indexOf,
  inArray : indexOf
}
}())
console.log( myarray.isArray([1,2]) ); //输出:true
console.log( myarray.isArray({0:1}) ); //输出:false
console.log( myarray.indexOf(['a','b','z'],'z') ); //输出:2
console.log( myarray.inArray(['a','b','z'],'z') ); //输出:2

console.log(myarray);

  

javascript私有方法揭示为公有方法

相关文章:

  • 2022-02-19
  • 2021-09-07
  • 2021-07-05
  • 2021-08-27
  • 2022-12-23
猜你喜欢
  • 2021-09-02
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2021-08-17
  • 2021-09-25
相关资源
相似解决方案