-1

I'm trying to include some namespace into my Javascript API.

Here's what I have so far:

if (!Sample || Sample == "undefined")
    var Sample = {};

Sample.SomeApi = {};

Sample.SomeApi.prototype = {
    SomeMethod: function() {
        alert('some api');
    }
};

What's going on here?

When I'm calling Sample.SomeApi.SomeMethod(); // it won't work as it will complain:

Uncaught TypeError: Object #<Object> has no method 'SomeMethod'
(anonymous function)Course:43
onclick
3
  • Side note: you want undefined or typeof Sample === 'undefined' not "undefined". Commented Jan 10, 2012 at 5:52
  • Well, Sample == "undefined" is not going to test if it is undefined, it is going to test whether Sample equals the string "undefined." Commented Jan 10, 2012 at 6:03
  • 1
    Good point, alternatively, he can do Sample == undefined but then again the whole undefined is unnecessary, that's what !Sample is testing for anyway. Commented Jan 10, 2012 at 6:33

3 Answers 3

2

I think you meant __proto__ not prototype, like so:

if (!Sample || Sample == "undefined")
    var Sample = {};

Sample.SomeApi = {};

Sample.SomeApi.__proto__ = {
    SomeMethod: function() {
        alert('some api');
    }
};

Sample.SomeApi.SomeMethod();

You can test it out in this jsFiddle: http://jsfiddle.net/luisperezphd/xtyyf/

But why not just this:

if (!Sample || Sample == "undefined")
    var Sample = {};

Sample.SomeApi = {
    SomeMethod: function() {
        alert('some api');
    }
};

Sample.SomeApi.SomeMethod();

It's much cleaner.

And there is the jsFiddle for that: http://jsfiddle.net/luisperezphd/HUuMQ/

Sign up to request clarification or add additional context in comments.

2 Comments

I'm not so sure about that. That would mean that you assign SomeMethod to the prototype of {}?
Using __proto__ works because objects do not have a prototype, functions do. However __proto__ is not standard and won't work across browsers, that is what getPrototypeOf is for. But the second method really is much cleaner.
1

You have to create a constructor for Sample.SomeApi not assign an empty object:

if(typeof Sample == 'undefined')
    Sample = {};

Sample.SomeApi = function() {
};

Sample.SomeApi.prototype = {
    someMethod : function() {
            alert('some api');
    }
}

new Sample.SomeApi().someMethod();

2 Comments

how would you make it call it like this: Sample.SomeApi.someMethod(); instead of Sample.SomeApi().someMethod(); ?
Well you don't need to use the prototype if your API is only a single object. just use Sample.SomeApi = { SomeMethod: function() { alert('some api'); } );
0

Try this

if(typeof Sample == 'undefined')
        var Sample ={}
    Sample.SomeApi = function(a)
    {
        this.a =a;
    }

Sample.SomeApi.prototype.SomeMethod = function() {
        alert('some api');
    }
    var d = new Sample.SomeApi(1);
    d.SomeMethod();

Hope this helps you

Go through this link here

Comments

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.