tags:

views:

46

answers:

2

Hi,

I have some simple OO code I've written that I'm playing with:

//define a constructor function
function person(name, sex) {
    this.name = name;
    this.sex = sex;

} 

//now define some instance methods
person.prototype.returnName = function() {
    alert(this.name);
}

person.prototype.returnSex = function() {
    return this.sex;
}

person.prototype.talk = function(sentence) {
    return this.name + ' says ' + sentence;
}

//another constructor
function worker(name, sex, job, skills) {
    this.name = name;
    this.sex = sex;
    this.job = job;
    this.skills = skills;
}

//now for some inheritance - inherit only the reusable methods in the person prototype
//Use a temporary constructor to stop any child overwriting the parent prototype 

var f = function() {};
f.prototype = person.prototype;
worker.prototype = new f();
worker.prototype.constructor = worker;

var person = new person('james', 'male');
person.returnName();
var hrTeamMember = new worker('kate', 'female', 'human resources', 'talking');
hrTeamMember.returnName();
alert(hrTeamMember.talk('I like to take a lot'));

Now this is all well and good. But I'm confused. I want to include namespacing as part of my code writing practice. How can I namespace the above code. As it is now I have 2 functions defined in the global namespace.

The only way I can think to do this is to switch to object literal syntax. But then how do I implement the pseudo-classical style above with object literals.

+1  A: 

You could for example do following:

var YourObject;
if (!YourObject)
  YourObject = {};

  YourObject.Person = function(name, sex) {
    // ...
  } 

  YourObject.Person.prototype.returnName = function() {
    // ...
  }

  // ...
}
RoToRa
Ah, of course. Thanks
elduderino
+1  A: 

You don't have to use object literals, at least, not exclusively.

  1. Decide on the single global symbol you'd like to use.
  2. Do all your declaration work in an anonymous function, and explicitly attach "public" methods as desired to your global object:

    (function(global) {
       // all that stuff
    
    
       global.elduderino = {};
       global.elduderino.person = person;
       global.elduderino.worker = worker;
    })(this);
    

I may not be completely understanding the nuances of your issue here, but the point I'm trying to make is that Javascript makes it possible for you to start with your symbols being "hidden" as locals in a function, but they can be selectively "exported" in various ways.

Pointy
I presume that iglobal should be global (or vice versa)?
jmar777
oh sorry - just a typo!
Pointy
By exporting do you mean make public via a return statement?
elduderino
No, though I suppose you could do things that way. What I mean is that you can build a function *inside* another function, and it's totally private. Then, if you want to, you can assign it as a property to a global variable. That's what I mean by "export": explicitly making a function visible after the function in which it was defined was returned.
Pointy
Basically RoToRa's answer is pretty much the same idea.
Pointy