(另一个)简单的JS OO框架(由python程序员提供) [英] (Yet another) simple JS OO framework (by a python programmer)

查看:121
本文介绍了(另一个)简单的JS OO框架(由python程序员提供)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是一名Python程序员,刚刚开始使用javascript。在阅读

一些js指南,并不喜欢我看到的任何OO使用模式时,

我已经烹饪了一些python人可能会发现的味道。


这里是代码 - 首先是引擎,然后是一些代码,展示了

的使用模式。


对于我(也许对你们中的一些人来说),它提升了可读性和一些经典OO语言的模式相似度。


干杯

aum

***开始代码***


//这是所有''引擎'' - 使用模式遵循


Object.prototype.subclass = function(props){


//从属性中提取''init''函数列表,或者

//使用null默认函数

if(props.hasOwnProperty(''init'')){

constructor =道具[''init'']

}

else {

constructor = function(){}

}


//设置继承

constructor.prototype = new this();


//使用除init之外的所有属性填充原型对象

for(key in props){

if(key!=''init''&& key!=''subclass''){

// alert(" Inheriting property''" + key +"''");

构造函数.prototype [key] =道具[key];

}

}


//完成

返回构造函数;

}


//使用模式


//创建''员工'' class

employee = Object.subclass({


init:function(name){

this.name = name; < br $>
},


getname:function(){

返回this.name;

},


toString:function(){

return" [employee:" + this.name +"]" ;;

}

})


//创建工程师课程

engineer = employee.subclass({


init:function(name,project){


//调用父初始化器

employee.apply(this,[name ]);


//做本地初始化

this.project = projec t;

},


getproject:function(){

返回this.project;

},


toString:function(){

return" [engineer:" + this.name +"(" + this.project +")];

}


})


//创建''工程师'助理''班级

助理= engineer.subclass({


init:function(姓名,老板){


//调用父初始化器

engineer.apply(this,[name,boss.project]);


这个。老板=老板

},


getboss:function(){

返回this.boss;

},


toString:function(){

return" [assistant:" + this.name +"(报告给&); + this.boss +")];

},

})


alert("构建员工''fred''...");

fred =新员工(Fred Dagg)

alert(构建工程师''mary'' ......");

mary =新工程师(Mary Smith,网络)


alert(构建助手) 吉姆 ...&现状t;);

jim =新助手(Jim Jones,mary)


alert(" fred.name =''" + fred.name +"''");

alert(" fred.getname()=''" + fred.getname()+"''");


alert(" mary.project =''" + mary.project +"''");

alert(" mary) .getproject()=''" + mary.getproject()+"''");


alert(" mary.name =''" + mary .name +"''");

alert(" mary.getname()=''" + mary.getname()+"''");


alert(" jim.name =''" + jim.name +"''");

alert(" jim.boss) =''" + jim.boss.name +"''");

Hi,

I''m a Python programmer, just starting to get into javascript. On reading
some of the js guides, and not liking any of the OO usage patterns I saw,
I''ve cooked up something which python folks might find to taste.

Here''s the code - first the ''engine'', then some code demonstrating the
usage patterns.

For me (and maybe for some of you), it promotes readability and some
pattern similarity for classical OO languages.

Cheers
aum
*** start code ***

// this is the ''engine'' of it all - usage patterns follow

Object.prototype.subclass = function(props) {

// extract ''init'' function from property list, or
// use a null default function
if (props.hasOwnProperty(''init'')) {
constructor = props[''init'']
}
else {
constructor = function() {}
}

// set up inheritance
constructor.prototype = new this();

// populate prototype object with all properties except init
for (key in props) {
if (key != ''init'' && key != ''subclass'') {
//alert("Inheriting property ''" + key + "''");
constructor.prototype[key] = props[key];
}
}

// done
return constructor;
}

// USAGE PATTERNS

// Create ''employee'' class
employee = Object.subclass({

init: function(name) {
this.name = name;
},

getname: function() {
return this.name;
},

toString: function() {
return "[employee: "+this.name+"]";
}
})

// Create engineer class
engineer = employee.subclass({

init: function(name, project) {

// invoke parent initialiser
employee.apply(this, [name]);

// do local initialisations
this.project = project;
},

getproject: function() {
return this.project;
},

toString: function() {
return "[engineer: "+this.name+"(" + this.project + ")]";
}

})

// Create ''engineer''s assistant'' class
assistant = engineer.subclass({

init: function(name, boss) {

// invoke parent initialiser
engineer.apply(this, [name, boss.project]);

this.boss = boss
},

getboss: function() {
return this.boss;
},

toString: function() {
return "[assistant: "+this.name + " (reporting to "+this.boss+")]";
},
})

alert("Constructing employee ''fred''...");
fred = new employee("Fred Dagg")
alert("Constructing engineer ''mary''...");
mary = new engineer("Mary Smith", "Networks")

alert("Constructing assistant ''jim''...");
jim = new assistant("Jim Jones", mary)

alert("fred.name=''" + fred.name + "''");
alert("fred.getname() = ''" + fred.getname() + "''");

alert("mary.project=''" + mary.project + "''");
alert("mary.getproject() = ''" + mary.getproject() + "''");

alert("mary.name=''" + mary.name + "''");
alert("mary.getname() = ''" + mary.getname() + "''");

alert("jim.name=''" + jim.name + "''");
alert("jim.boss=''" + jim.boss.name + "''");

推荐答案

aum写道:
aum wrote:

我是Python程序员,刚开始进入javascript。
I''m a Python programmer, just starting to get into javascript.



这让你成为判断javascript应该如何使用的最佳人选


Which makes you the best person to be judging how javascript should be
used?


在阅读一些js指南,并且不喜欢我看到的OO使用模式中的任何一个时,我已经做了一些东西

哪个python人们可能会发现品尝。
On reading some of the js guides, and not liking any of
the OO usage patterns I saw, I''ve cooked up something
which python folks might find to taste.



< snip>


尝试使javascript与其他类似的后果之一

编程语言作为那些努力奖励的人倾向于开始使用其他语言的期望和预先概念来开始,但最终会失望。对于那些采用javascript思维模式的人来说,当他们写了
javascript时,可以说很多。

<snip>

One of the consequences of trying to make javascript resemble other
programming languages as those rewarded by the effort tend to start
applying the expectations and pre-conceptions of that other language to
javascript, only to be ultimately disappointed. There is a great deal to
be said for people adopting a javascript mindset when they wrote
javascript.


***开始代码***


//这是所有的''引擎' - 使用模式如下


Object.prototype.subclass = function(props){
*** start code ***

// this is the ''engine'' of it all - usage patterns follow

Object.prototype.subclass = function(props) {



< snip>

<snip>


//设置继承

constructor.prototype = new this();
// set up inheritance
constructor.prototype = new this();



创建一个属性似乎有点不正常 -

Object.prototype - 只能以某种方式调用 - 这个 -

引用是对函数的引用。如果 -

子类 - 方法是 - Function.prototype - 的扩展,这会感觉更好。在这种情况下它也会起作用,但是这种改变也会带来优势

不修改 - Object.prototype - 添加一个可枚举的

财产。对Object.prototype的这样一个加法 - 对javascript对象的for-in-loops的所有使用都有影响,而枚举函数的

属性并不常见没问题。

It seems a little perverse to be crating a property of -
Object.prototype - that can only be called in a way where the - this -
reference is a reference to a function. This would feel better if the -
subclass - method was an extension of - Function.prototype -. It would
work as well in that case, but that change would also have the advantage
of not modifying the - Object.prototype - by adding an enumerable
property. Such an adition to Object.prototype - has implications for all
uses of - for-in - loops on javascript objects, while enumerating the
properties of functions is uncommon so much less of a problem.


//使用除init以外的所有属性填充原型对象

for(key in props){
// populate prototype object with all properties except init
for (key in props) {



< snip>


这里 - 键 - 尚未被声明为局部变量,所以所有

对它的赋值作为全局对象属性的赋值

(实际上是一个全局变量)。这是一个非常糟糕的做法。

一般编程公理,没有任何变量应该给予更多的范围

比它绝对需要的javascript和其他任何一样都是真实的

语言。并且,在对另一个对象进行子类化时,任何一个对象的子类都会产生混乱的结果,因为它们将共享 -

键 - 变量。 (这可能是一个不太可能的意外情况,但风险可以完全通过在当地谴责变量来消除



理查德。

<snip>

Here - key - has not been declared as a local variable, so all
assignments to it act as assignments to a property of the global object
(effectively a global variable). This is a very bad practice. The
general programming axiom that no variable should be given more scope
than it absolutely needs is as true in javascript as it is in any other
language. And chaotic results will follow from any attempt to subclass
one object while subclassing another, as they will be sharing the -
key - variable. (That may be an improbable contingency but risk can be
eliminated entirely by decrying the variable locally)

Richard.


On Sun,2006年9月17日15:23:40 +0100,Richard Cornford写道:


首先 - 感谢您的回复。
On Sun, 17 Sep 2006 15:23:40 +0100, Richard Cornford wrote:

Firstly - thanks for your reply.

>我是Python程序员,刚开始进入javascript。
>I''m a Python programmer, just starting to get into javascript.


哪个让你成为判断javascript应该如何使用的最好的人?
使用?
Which makes you the best person to be judging how javascript should be
used?



没办法 - 它让我成为一个对python非常熟悉的人,

用javascript感觉出了他的舒适区域,并且

摔跤如何用javascript编写代码的问题

类似的可读性和可理解性。


什么我正在尝试做的是呈现常见的javascript习语:


函数管理器(名称,部门){

this.base = Employee;

this.base(name,dept);

this.reports = [];

}

Manager.prototype =新员工;

Manager.prototype.addReport = function(员工){

this.reports.push(员工);

}


作为我(目前)发现更直观和可读的东西,例如:


经理=子类(员工,{


init:function(name,dept){

Employee.apply(this,[name,dept])

},

addReport:function(empl oyee){

this.reports.push(员工);

}

})

No way - it makes me someone who has gotten very comfortable with python,
who is feeling way out of his comfort zones with javascript, and is
wrestling with the issue of how to write code in javascript that is
similarly readable and understandable.

What I''m trying to do is render the common javascript idiom:

function Manager (name, dept) {
this.base = Employee;
this.base(name, dept);
this.reports = [];
}
Manager.prototype = new Employee;
Manager.prototype.addReport = function(employee) {
this.reports.push(employee);
}

as something I''m (presently) finding more intuitive and readable, eg:

Manager = subclass(Employee, {

init: function(name, dept) {
Employee.apply(this, [name, dept])
},
addReport: function(employee) {
this.reports.push(employee);
}
})


尝试使javascript类似于其他

编程语言的后果之一,因为那些努力所奖励的人倾向于开始使用预期和前概念那个其他语言对于b $ b javascript,只是最终失望了。
One of the consequences of trying to make javascript resemble other
programming languages as those rewarded by the effort tend to start
applying the expectations and pre-conceptions of that other language to
javascript, only to be ultimately disappointed.



我能理解并尊重它。


我接受网络浏览器开发组织不是关于

一起工作以支持客户端python脚本编写,所以我很快就会放松对python范例的依赖,并且

弄清楚如何让我的大脑思考javascript。

I can understand and respect that.

I do accept that the web browser development organisations are not about
to work together to support client-side python scripting in any great
hurry, so I''m going to have to relax my cling on the python paradigm, and
figure out how to get my brain thinking javascript.


对于采用a的人来说,有很多可以说是b $ b他们写的时候javascript心态

javascript。
There is a great deal to
be said for people adopting a javascript mindset when they wrote
javascript.



也许你可以推荐一些网站和门户网站,除了核心

语言和dom指南等,它们可以帮助其他语言的人们/>
背景欣赏''javascript-flavoured reality''。

Maybe you can recommend some websites and portals, beyond the core
language and dom guides etc, that help people from other language
backgrounds to appreciate ''javascript-flavoured reality''.


似乎有点反常地要装箱 -

Object.prototype - 只能以 - this -

引用是对函数的引用的方式调用。
It seems a little perverse to be crating a property of -
Object.prototype - that can only be called in a way where the - this -
reference is a reference to a function.



我可以轻松实现''subclass''作为全局函数接受

作为参数的基类和属性映射新课程(见下面的
)。

I could just as easily implement ''subclass'' as a global function accepting
as arguments the base class and a property mapping for the new class (see
below).


这里 - key - 尚未被声明为局部变量,所以

赋值给它作为全局对象属性的赋值

(实际上是一个全局变量)。
Here - key - has not been declared as a local variable, so all
assignments to it act as assignments to a property of the global object
(effectively a global variable).



感谢您的纠正 - 我还在想python,其中变量

是默认本地的,而不是全局的默认。我的不好。


无论如何,这是我修改过的实现:


函数子类(基础,道具){

var构造函数,键;


//从属性列表中提取''init''函数,或者

//使用null默认函数

if(props.hasOwnProperty(''init'')){

constructor = props [''init'']

}

else {

constructor = function(){}

}


//设置继承

constructor.prototype = new base();


之外的所有属性(键入道具){

if(key!=''init''){

constructor.prototype [key] =道具[key];

}

}


返回构造函数;

}


作为javascript新手,我确实向这个群体开放,以获得建设性的建议和反馈。


干杯

aum

Thanks for the correction - I was still thinking python, where variables
are local-by-default, not global-by-default. My bad.

Anyway, here''s my amended implementation:

function subclass(base, props) {
var constructor, key;

// extract ''init'' function from property list, or
// use a null default function
if (props.hasOwnProperty(''init'')) {
constructor = props[''init'']
}
else {
constructor = function() {}
}

// set up inheritance
constructor.prototype = new base();

// populate prototype object with all properties except init
for (key in props) {
if (key != ''init'') {
constructor.prototype[key] = props[key];
}
}

return constructor;
}

As a javascript neophyte, I do throw myself open to this group for
constructive suggestions and feedback.

Cheers
aum


我已经写了我的子类计划:

http://www.freenet.org.nz/misc/jsoo.html


干杯

aum
I''ve written up my subclassing scheme at:

http://www.freenet.org.nz/misc/jsoo.html

Cheers
aum


这篇关于(另一个)简单的JS OO框架(由python程序员提供)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆