0

I admit this question is getting to the limits of what I know of JavaScript & jQuery, and there is probably a more proper way to state my question (which would help in finding an existing solution), but if you can bear with me, this is what I'm after.

I have an existing object class I've defined. I'm making a jQuery ajax call using getJSON, and I want my callback parameter (which is an object) to be classed as my custom object, so that I can access that class' methods from it.

So I have some object class

function Boo() {

  this.param1;
  this.param2;

  this.yah = function() {
     ...
  }

}

and then I have something elsewhere of the sort

$.getJSON(url,function(new_instance) {
   //from my php source this passed object is already loaded with param1, param2...
   alert(new_instance.param1);    //no probs
   //but i want to be able to then call
   new_instance.yah();
});

In other words, I want new_instance to be considered an instance of Boo(). I know in stuff like ActionScript you have to class the incoming parameters for exactly this reason, dunno what flexibility I have in JS.

I thought maybe about having an intermediate function that takes in the incoming object and creates/populates a new instance of Boo() but not sure if there is a more clever method.

Many thanks!!

1
  • To both responses so far, yes I know I should be prototyping (;D), you may assume that my final product will be more efficiently written... Commented Jan 21, 2012 at 15:49

2 Answers 2

1

Do not define methods in the constructor function, you are defining them over and over again every time the costructor is called. Move them over to the prototype:

Boo.prototype = {

    yah: function() {

    },

    bah: function() {

    }

    ...

};

a little helper function:

function coerceTo( proto, values ) {
    var r = Object.create( proto );
    for( var key in values ) {
        r[key] = values[key];
    }

    return r;
}

Depending on browser, Object.create might not be available, so:

if (!Object.create) {
    Object.create = function (o) {
        if (arguments.length > 1) {
            throw new Error('Object.create implementation only accepts the first parameter.');
        }
        function F() {}
        F.prototype = o;
        return new F();
    };
}

Usage:

new_instance = coerceTo( Boo.prototype, new_instance );

new_instance instanceof Boo //true
new_instance.yah();
Sign up to request clarification or add additional context in comments.

1 Comment

Here's what I did, it isn't properly prototyped but the general idea is there... <code>Boo.prototype.dp(params,callback) { $.getJSON(params,function(r){ if (typeof(r) == 'object') { //this line i realize can be written better as above var t = new Boo(); //loop and load up params here (not shown) callback(t); } else { callback(r); } }); another_instance.dp(params,function(new_instance) { new_instance.yah(); //hooray! } );</code>
0

What you can do:

$.getJSON(url,function(newObjData) {
   var newObj = $.extend(new Boo(), newObjData);
   newObj.yah();
});

Also consider moving your Boo methods to object prototype so the methods don't get recreated for each Boo instance:

var Boo = function() {
    this.param1;
    this.param2;
}

Boo.prototype.yah = function() {
    console.log(this.param1);
}

3 Comments

Calling the original constructor like this can cause side effects or errors.
OK many thanks for both answers, this one looks ideally low-budget, granted as I'm building an API I was hoping to be extra fussy and somehow let the users just use myfunction(params,function(boo){ .. } and use 'boo' as a Boo() right away in their scripts, but probably asking too much!
All depends on your needs. But I should admit that the answer of @Esailija is conceptually correct and more robust.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.