JavaScript迷惑问题之三:如何区分类的静态成员和类的原型对象的成员?

Patrick_DK 2002-04-23 09:58:23
我感觉两个差不多啊,都是保存公用的属性和方法的,到底区别在哪里呢?
...全文
148 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lostinet 2002-04-26
  • 打赏
  • 举报
回复
JS没有静态成员。
要调用一个方法,必须有类的Instance。
Math,...等都是Global的成员来的。
例如:
_Math=Math;
Math=new Object();
Math.max=(function(a,b){
alert("Call Math.max("+a+","+b+")");
return _Math.max(a,b);
})
alert(Math.max(33,44));
Lostinet 2002-04-26
  • 打赏
  • 举报
回复
JS没有静态成员。
要调用一个方法,必须有类的Instance。
Math,...等都是Global的成员来的。
例如:
_Math=Math;
Math=new Object();
Math.max=(function(a,b){
alert("Call Math.max("+a+","+b+")");
return _Math.max(a,b);
})
alert(Math.max(33,44));
flylyke 2002-04-24
  • 打赏
  • 举报
回复
到.net就有了,不过还是差一点,有几个例子:
具有字段的类
class myClass {
const answer : int = 42; // Constant field.
var distance : double; // Variable field.
}

var c : myClass = new myClass;
c.distance = 5.2;
print("The answer is " + c.answer);
print("The distance is " + c.distance);
具有方法的类
class myClass {
const answer : int = 42; // Constant field.
var distance : double; // Variable field.
function sayHello() :String { // Method.
return "Hello";
}
}

var c : myClass = new myClass;
c.distance = 5.2;
print(c.sayHello() + ", the answer is " + c.answer);
具有构造函数的类
class myClass {
const answer : int = 42; // Constant field.
var distance : double; // Variable field.
function sayHello() :String { // Method.
return "Hello";
}
// This is the constructor.
function myClass(distance : double) {
this.distance = distance;
}
}

var c : myClass = new myClass(8.5);
print("The distance is " + c.distance);
huojiehai 2002-04-24
  • 打赏
  • 举报
回复
js本来就没有类这一说,只有对象
孟子E章 2002-04-24
  • 打赏
  • 举报
回复
js对类的支持不是很强。

87,994

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧