0

I'm trying to make a module wich have properties and functions, that can be used like a validator object that valid all the objects inside, with a valid method that return true if validator success.

so i make this file

function Machine(params)
{
    // this is the constructor
    if(params){
        var pub=params;
        return this.init(pub);
    }
    this.obj_params = 'null';
    this.valid = 'Not Valid';
};
Publicacion.prototype.init = function(objConfig){
    console.info('Init Success!')
    this.buildMachine(objConfig);

    return true
};
Publicacion.prototype.buildPublish = function(objConfig){
    console.info('Builded!');
    //this.valid='success'; // when uncommited, the object this.valid appears

    return true;
};

module.exports=Machine;

and this is the console

> var Machine=require('./Machine')
> undefined
> var machinegun=new Machine();
> Init Success!
> Builded!
> undefined
> machinegun.valid
> undefined

two problems:

  1. When i try to access to "machinegun.valid" this returns me a undefined
  2. When i use the build method to define valid, the var valid appears.

why the constructor did not define the valid variable at first?? why valid variable can be defined by the build method???

i dont understand how javascript works with classes...

thnx all!

andrescabana86
  • 1,778
  • 8
  • 30
  • 56
  • How would that invocation (`new Machine()`) call the `init` method (and log `Init Success!`) if no `params` are given (`undefined`)? – Bergi Aug 24 '13 at 19:56
  • [JavaScript has no classes](http://stackoverflow.com/a/13418980/1048572). Constructors are still simple functions, and when you `return` from them they will exit without executing further code (in here, creating properties). Simply remove `return`. – Bergi Aug 24 '13 at 19:58

3 Answers3

2

The function returns this.init(pub) before being able to set this.valid. You should define this.valid first in the constructor function.

Herman Tran
  • 1,581
  • 2
  • 12
  • 19
1

You are skipping an else in there. The logic is that if params are passed, use them to initiate, otherwise set two "no params" properties:

function Machine(params)
{
    // this is the constructor
    if(params){
        var pub=params;
        return this.init(pub);
    }
    else {
      this.obj_params = 'null';
      this.valid = 'Not Valid';
    }
};
itmitica
  • 501
  • 3
  • 10
  • Otherwise the code doesn't make sense, at least for me. It doesn't make any sense to have this.obj_params = 'null' while at the same time using the params to initialize. – itmitica Aug 24 '13 at 20:22
0

hmmm,first

Publicacion.prototype.

should probably be

Machine.prototype

and Publicacion.prototype.buildPublish

should be

Machine.buildMachine

but that's probably not what you meant.

the simple reason that valid returns false for you is that you're not defining it - you're returning from the function before that.

just change the order:

function Machine(params)

this.obj_params = 'null';
this.valid = 'Not Valid';
{
    // this is the constructor
    if(params){
        var pub=params;
        return this.init(pub);
}

};

shaish
  • 1,479
  • 1
  • 12
  • 23
  • error typing in Publicacion, i modify Publicacion with Machine to simplify the question, my bad! but thank you and thank you for the explanation, im returnning the function before define. – andrescabana86 Aug 24 '13 at 20:01