JavaScript基于面向对象的代码演示:
//定义People类 var People = function(){ //私有属性 var _age=20; //公开属性 this.age=_age; this.sex=""; //公开方法 this.sayHello=function(){ alert("People say:\"Hello\""); }; this.getSex=function(){ return this.sex; }; //私有方法 function say(){ alert("People say:\"Hello js private method\""); } }; //静态属性 People.eyesCount=2; //静态方法 People.getHandsCount = function(){ return 2; }; //实例化类 var p = new People(); //调用公开的方法 p.sayHello(); //调用静态属性 alert("eyes count:"+People.eyesCount); //定义Man类 var Man = function(){ this.haha="Hahaha"; //方法重写 this.getSex=function(){ return "male"; }; }; //类的继承 Man.prototype=new People(); //方法重写 Man.prototype.sayHello=function(){ alert("Man say:\"Hello Js\""); }; var m = new Man(); m.sayHello(); alert(m.getSex());
上面那段代码将alert改成trace后可以完全放在 flash 使用as1.0来测试,完全没错。因为as1.0 就是脱胎于js。
只不过 as与js 在不同的运行环境里不同。就其纯粹的语言本身并无二致。