对象方法就像属性一样 [英] object methods act like properties

查看:61
本文介绍了对象方法就像属性一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,如果我可以使对象方法看起来像一个

对象属性。


例如:

函数Circle(x,y,r){this.x = x; this.y = y; this.r = r; }

Circle.prototype.area = function(){return Math.PI * this.r * this.r; }


var c = new Circle(0,0,1);


我想通过调用它获得该区域:var area = c.area;


而不是c.area();


------------ ----------


我明白这是糟糕的设计,但我很好奇,如果它可以完成

。 br />

I was curious if I can make an object method look and act like an
object property.

example:

function Circle(x,y,r){ this.x = x; this.y = y; this.r = r; }
Circle.prototype.area = function(){ return Math.PI * this.r * this.r; }

var c = new Circle(0,0,1);

I would like to get the area by calling it: var area = c.area;

instead of c.area();

----------------------

I understand that this is bad design, etc but I am curious if it can be
done.

推荐答案

一般来说我不这么认为。


Mozilla是一个例外,使用原型.__ defineGetter__和

原型.__ defineSetter__方法,这将完成您想要的任务。


我不知道其他浏览器支持这个。

Generally I don''t think so.

Mozilla is an exception, with the prototype.__defineGetter__ and
prototype.__defineSetter__ methods, which will do just what you want.

I don''t know what other browsers support this.





Nathan White写道:


Nathan White wrote:
我是好奇,如果我可以使对象方法看起来像一个
对象属性。

例如:

函数Circle(x,y,r){this。 x = x; this.y = y; this.r = r; }
Circle.prototype.area = function(){return Math.PI * this.r * this.r; }

var c = new Circle(0,0,1);

我想通过调用它获得该区域:var area = c.area;

而不是c.area();
I was curious if I can make an object method look and act like an
object property.

example:

function Circle(x,y,r){ this.x = x; this.y = y; this.r = r; }
Circle.prototype.area = function(){ return Math.PI * this.r * this.r; }

var c = new Circle(0,0,1);

I would like to get the area by calling it: var area = c.area;

instead of c.area();




使用Mozilla的JavaScript,你可以创建getter(当然还有setter) b $ b但你的例子不需要):


函数Circle(x,y,r){this.x = x; this.y = y; this.r = r; }

Circle.prototype .__ defineGetter __(''area'',function(){return Math.PI *

this.r * this.r;});


var c = new Circle(0,0,1);

alert(c.area);


但这不是便携式的,因此对于网上的脚本来说无用。

general。但Mozilla的innerHTML实现只不过是一个适当定义的

getter / setter。


-


Martin Honnen
http://JavaScript.FAQTs.com/


实际上我找到了一种跨浏览器的方式来使用

关闭来解决这个问题:


function Circle( x,y,r){

var self = this;

this.x = x;

this.y = y;

this.r = r;


this.area =(function(){return Math.PI * self.r * self.r;})() ;

}


唯一的事情就是当我使用

原型链时似乎搞得一团糟:


Circle.prototype.area =(function(){return Math.PI * self.r * self.r;

})();


任何想法如何在原型链中使用闭包来完成上面的

结果?

Actually I found a cross browser way of pulling this off using
closures:

function Circle(x,y,r){
var self = this;
this.x = x;
this.y = y;
this.r = r;

this.area = (function(){ return Math.PI * self.r * self.r; })();
}

the only thing is it seems to get all messed up when I use the
prototype chain, for example:

Circle.prototype.area = (function(){ return Math.PI * self.r * self.r;
})();

any idea how to use a closure in a prototype chain to accomplish the
result above?


这篇关于对象方法就像属性一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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