Matías Cánepa
Matías Cánepa

Reputation: 5974

pass parameter to a prototyped function in javascript?

How can I pass an object from a function to a protoyped function of it's own?

function Main()
{
    this.my_object = {"key":123};
}

Main.prototype.Sub = new Sub(this.my_object);

function Sub(obj)
{
    alert(obj);
}

Main.Sub; //this should alert the object created in Main()

fiddle: http://jsfiddle.net/GkHc4/

EDIT 1:

I'm trying to make a chain of functions, and each link must get the previous object and add something. At this point it is an experiment. For example:

Main.Link1.Link2.link3();

//link3 it's a prototype for link2
//link2 it's a prototype for link1
//and so on...

Where each link adds a key to the initial object

Upvotes: 3

Views: 4605

Answers (3)

basilikum
basilikum

Reputation: 10526

There are three different issues:

1) You don't create an object with new Main(), but try to access the Sub property directly from the constructor. This doesn't work. You have to create an instance:

var main = new Main();
main.Sub; //<-- now you can access it 

2) You try to access the property my_object with this, but outside of any function. That doesn't work either. this will probably point to the window object, which doesn't have any property called my_object. The solution could be to write main.my_object but that would kind of defeat the purpose of the prototype. Usally you would put there function or properties that are the same for every instance. But you are trying to put a property in there that should be different for every instance. So it looks like you don't need to access the prototype at all but can just define it as a regular property:

function Main()
{
    this.my_object = {"key":123};
    this.Sub = new Sub(this.my_object);
}

3) The line main.Sub doesn't execute anything. You are just requesting the property Sub. Instead the function Sub will be executed when you write new Sub(...). So if you want to alert something by calling a function, you have to define a function. You could for instance define an alert method in Sub or in Sub.prototype and then call this method:

function Sub(obj)
{
    this.alert() {
        alert(obj);
    }
}

main.Sub.alert();

updated Fiddle

Upvotes: 2

Andrew Clark
Andrew Clark

Reputation: 208485

I think that maybe you are looking for something like the following:

function Main()
{
    this.my_object = {"key":123};
}

Main.prototype.Sub = function () {
    Sub(this.my_object);
};

function Sub(obj)
{
    alert(obj);
}

var main = new Main();  // main object is created with main.my_object property
main.Sub();  // this will do alert(main.my_object)

Upvotes: 1

Deleteman
Deleteman

Reputation: 8700

I think you're going at it in the wrong way.. you see:

  • The alert is not coming from the last line, it's actually coming from the prototype line, when you do the "new Sub".

Maybe a better approach would be something like:

function Main()
{
    this.my_object = {"key":123};
}

Main.prototype.Sub =  Sub; //You set the prototype, but don't actually execute the function

function Sub(obj)
{
    alert(obj);
}

var m = new Main(); //You need to create a new object of type Main in order for it to have access to the method Sub
m.Sub(m.my_object); //this should alert the object created in new Main()

Does this help?

Edit

Additionally, you could even do something like this for the Sub function:

function Sub() {
   alert(this.my_object);
}

Although that way, you wouldn't be able to use the function by itself.

Upvotes: 1

Related Questions