У меня есть простой OO-код, который я написал, с которым я играю:
//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'));
Теперь все хорошо.Но я в замешательстве.Я хочу включить пространство имен как часть моей практики написания кода.Как я могу именовать пространство вышеупомянутым кодом.Как и сейчас, у меня есть 2 функции, определенные в глобальном пространстве имен.
Единственный способ, которым я могу думать, это переключиться на синтаксис литерала объекта.Но тогда как мне реализовать описанный выше псевдоклассический стиль с помощью литералов объектов.