在javascript中正确创建具有函数的object_helper

Creating an object_helper with functions properly in javascript

本文关键字:函数 object helper javascript 创建      更新时间:2023-09-26

我正计划在js:中创建一个这样的对象助手

var person_helper = {
   isAlive : function(person) {
      ...
   },
   isHisNameIs : function(person,name) {
      ...
   },
   isSeniorCitizen : function(person) {
   }
}

我这样称呼帮助者:

person_helper.isAlive(person_object); 
person_helper.isHisNameIs(person_object,"Dan"); 
person_helper.isSeniorCitizen(person_object);

现在,我的问题是:由于我在person-helper中使用person对象,并且我可能总是一遍又一遍地使用同一个对象-有没有一种方法可以用这样的方式编写帮助程序?:

person_helper(person_object).isAlive();
person_helper(person_object).isHisNameIs("Dan");
person_helper(person_object).isSeniorCitizen();
  1. 这样写有逻辑吗?(主要是为了避免在定义函数时通过每次"人"对象)
  2. 我该如何写才能工作

您必须在助手中添加一个函数,并使用父函数的变量。

var person_helper = function(person) {
    var parent = this;
    this.name = person.name ;
    this.isHisNameIs = function(name) {
        if(name == parent.name)
            console.log('OK');
        else
            console.log('NOP');                    
    }
}

http://jsfiddle.net/H4RsJ/6/

在我看来,创建一个person_helper不是一个好主意。相反,您应该创建一个Person原型,请阅读本文了解更多信息。

你应该有像Name和Alive这样的成员,你可以根据你的需求实现你的功能。

我同意其他人的说法,即这些方法应该是person对象的一部分。我认为这会更有意义。

但为了好玩,你想要的东西与undercore.js提供的东西类似。所有方法都可以用函数的方式调用,但也可以包装一个对象/数组,以面向对象的方式调用这些方法。

为了实现这一点,您必须将person_helper定义为一个函数,将这些方法分配给它的原型,并将其本身分配为静态方法:

var person_helper = (function() {
    var methods = {
       isAlive : function(person) {},
       isHisNameIs : function(person,name) {},
       isSeniorCitizen : function(person) {}
    };
   var Helper = function(person) {
       if(!(this instanceof Helper)) { // so we can call the function with `new`
           return new Helper(person);    
       }
       this.person = person;
   };
   // set up instance and static methods
   for(var m in methods) {
       (function(m) { // instance methods setup
            Helper.prototype[m] = function() {
                 // call the original method, passing `this.person` as
                 // first argument
                 return methods[m].apply(null, [this.person].concat(arguments));
            };
        }(m));
       Helper[m] = methods[m]; // static method
   }
   return Helper;
}());

然后您可以将其用作:

person_helper.isHisName(person, 'Dan');
person_helper(person).isHisName('Dan');

DEMO

您可以在W3Schools.com网站的Javascript对象部分找到非常好的东西。重点是,您可以创建一个对象,将其存储为个人的属性_helper:

var person_helper = {
    ...
    /** which is the same than create a new instance of Person Object. */
    person : function person(firstname,lastname,age,eyecolor) {
        this.person = {};
        this.person.firstname=firstname;
        this.person.lastname=lastname;
        this.person.age=age;
        this.person.eyecolor=eyecolor;
    },
    ...
};

您将在助手属性中检索person。缺点是必须为每个person_helper管理person对象。但这不是什么大问题。

使用Object.create()继续您的示例:

var person_helper = function(newp) {
   return Object.create({
       person : newp,
       isAlive : function() {
           println(this.person + " is alive");
       },
       isHisNameIs : function(name) {
           println(this.person + " name is " + name);
       },
       isSeniorCitizen : function() {
           println(this.person + " is getting on in age..");
       }});
};
var person_object = "some guy";
person_helper(person_object).isAlive(); 
person_helper(person_object).isHisNameIs("Dan"); 
person_helper(person_object).isSeniorCitizen();

使用jdk8版本的jrunscript-prints:运行此程序

some guy is alive 
some guy name is Dan
some guy is getting on in age..

您还可以选择将person_helper()的结果视为对象以避免重新构建:

var personObj = person_helper(person_object);
personObj.isAlive();    
personObj.isHisNameIs("Dan"); 
personObj.isSeniorCitizen();