0

I am trying to create an api that extends some functionality of Tizen.

Tizen has a way of creating objects such as: 'new tizen.ContactName(...)' and 'addressbook = tizen.contact.getDefaultAddressBook();'.

This seems to be a nice way to group together methods and objects when there are a lot of them.

So, for example I want to extend the contact handling:

(An external js-file)

function ContactManager(){ //edited by comment
    var self = this;

    this.add = function(details, posCallback, negCallback){
    //do stuff to add contact

    this.otherMethod(){...}
}

etc.

I can call this by using: var contactManager = new ContactManager(); and it works fine. Now I want to access by include it in another object(?) so that it looks like: var contactManager = new myTizen.ContactManager().

I tried:

function myTizen(){

this.ContactManager = function(){
    //methods and stuff
    }
}

This doesn't work. Why? How should I build my "API"?

Yokich
  • 1,247
  • 1
  • 17
  • 31

2 Answers2

1

I see it like this

define some object myTizen

then set myTizen.ContactManager = somefunction();

Chris Jones
  • 662
  • 2
  • 10
  • 23
  • Is that not what I have done? this.Contactmanager = function(){} is the same thing, just constructed from within. I could be wrong, not so good at javascript yet – Yokich Mar 25 '14 at 13:05
  • I dunno, I guess it looks similar. I am still not great at JavaScript either. I just know that I can't say I've seen anything like myTizen.ContactManager(); Then again, maybe I need more experience. – Chris Jones Mar 25 '14 at 13:07
  • Why do you want to include it in another object if you are going to be naming it the same thing? To me it just seems like you could just keep the 'var contactManager = new ContactManager();' and extend it as you please – Chris Jones Mar 25 '14 at 13:11
  • @ChrisJones, this sort of thing can be useful to do some psuedo namespacing of your classes and functions. If you're building a parent framework or controller class and you want to be explicit that the functionality you're using is part of that overarching system, then this is a way to do that. – Jason Mar 25 '14 at 13:21
  • Well then ok!!! I guess I wasn't really clear on what he was wanting overall, so that probably contributed some. – Chris Jones Mar 25 '14 at 13:32
  • All this does is set `myTizen.ContactManager` to the result of `somefunction()`... which, if `somefunction()` returns a function that you can instantiate with `new`, then I guess this could be what you're looking for. – Jason Mar 28 '14 at 13:19
1

Here's what you want:

function myTizen() {
    function whatevername() {
        // methods and stuff
    }
    // you can even extend whatevername's prototype down here

    this.ContactManager = whatevername; // please note the lack of parentheses
}

// here's another way you could do it:
function alternateMyTizen() {
}

function alternatewhatever() {
    // methods and stuff
}
// extend the prototype if you choose

alternateMyTizen.prototype.ContactManager = alternatewhatever;

The main difference between option 1 and option 2 is that in the second method, your "subclass" remains in scope and can be used independently of your myTizen class, in the first method once the constructor goes out of scope, you can only access it through myTizen.ContactManager.

Jason
  • 13,606
  • 2
  • 29
  • 40
  • This is not what I wanted. I would still not be able to call "new myTizen.whatevername()" and why is actually obvious- you cant call a functions function. – Yokich Mar 28 '14 at 07:14
  • But... but you don't want to call `new myTizen.whatevername();`, you want to call `new myTizen.ContactManager();`. Did you actually try my example? Cuz I just did, and it works exactly the way you wanted it to. – Jason Mar 28 '14 at 13:15