Alon
Alon

Reputation: 7758

Creating an object_helper with functions properly in javascript

I am planning to create an object helper like this in my js:

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

   }
}

This way I am calling the helper like this:

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

Now, My question is: since I am using the person object in the person helper and I will probably always use the same object over and over again - Is there a way to write the helper in a way that I can use it like this?:

person_helper(person_object).isAlive();
person_helper(person_object).isHisNameIs("Dan");
person_helper(person_object).isSeniorCitizen();
  1. Does it make any logic to write it this way? (mainly to avoid passing each time "person" object when defining the function)
  2. How do I write it so it will work?

Upvotes: 3

Views: 3910

Answers (5)

pd40
pd40

Reputation: 3257

Continuing your example using 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();

Running this with the jdk8 version of jrunscript prints:

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

You also have the option of treating the result of person_helper() as an object to avoid re-construction:

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

Upvotes: 0

BendaThierry.com
BendaThierry.com

Reputation: 2109

You will find very good stuff about that on the W3Schools.com website, Javascript Object Section. The point is you can create an Object you will store as a property of your person_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;
    },
    ...
};

And you will retrieve personinside your helper properties. The downside is you have to manage the person object foreach person_helper. But it is not a big problem.

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816790

I agree with others saying those methods should be part of the person object. It would make more sense I think.

But for the fun of it, what you want is something similar than what underscore.js offers. All methods can be called in a functional way, but you can also wrap an object / array to call those methods in an object oriented way.

For that to work, you have to define person_helper as a function, assign these methods to its prototype and to itself as static methods:

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;
}());

Then you can use it as:

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

DEMO

Upvotes: 0

Genosite
Genosite

Reputation: 359

You must add a function into your helper and use the variable of parent function.

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/

Upvotes: 1

Lajos Arpad
Lajos Arpad

Reputation: 76817

In my opinion it is not such a good idea to create a person_helper. Instead you should create a Person prototype, read this for more information.

You should have members such as Name and Alive and you can implement your functions based on your requirements.

Upvotes: 0

Related Questions