JavaScript基础 实例和框架集成

完整的例子: 使用对象和继承等的示范

//父类示例
function Parent(students, teacher) {
       //公有成员
       this.name = teacher;
       this.students = students;
       this.teacher = teacher;
       //私有成员
       //_me 处理作用域的特殊变量
       var _me = this;
       var _year;
       //构造函数定义的末尾执行
       function constructor() {
              _year = 1900;
       }
       //公有方法
       this.getName = function () {
              this.pubMe1();
              return this.name;
       };
       //公有方法,特权方法可以访问私有和公有成员
       this.getYearBorn = function () {
              _disp();
              return _year;
       };
       //私有方法
       function _disp(){
              alert(_me.name);
              alert(_year);
       }
       var _disp2 =function(){
              alert(_me.name);
              alert(_year);
       };
 
       constructor();
}
//公有方法的另一种定义
Parent.prototype.pubMe1 = function () {
       //只能访问实例成员
       alert(this.name);
};
//静态成员和方法
Parent.TITLE = 'static';
Parent.StaticMe = function () {
       alert(this.TITLE);
};
 
//子类示例
function Child(students, teacher) {
       //构造传递
       Parent.apply(this, arguments); 
 
       this.cld = students;
       var _pri = teacher;
 
       this.DoChild = function () {
              alert(this.cld + _pri);
              alert(this.name);
       };
}
//原形继承法
Child.prototype = new Parent();
Child.prototype.constructor = Child;
 
// Create a new Parent object
var p = new Parent(["John", "Bob"], "Mr. Smith");
alert(p.getName());
alert(p.getYearBorn());
p.pubMe1();
Parent.StaticMe();
 
var c = new Child("c", "p");
c.DoChild();

 

完整例子:函数对象的扩展封装形式

function Parent(name) {
       var _pri = "pri" + name;
 
       var priMethod = function(){
              alert(_pri);
       };
 
       return{
              pub : name,
 
              pubMethod : function () {
                    alert(this.pub);
                    alert(_pri);
                    priMethod();
              }
       };
}
 
Parent.TITLE = 'static';
Parent.StaticMe = function () {
       alert(this.TITLE);
};
 
alert(Parent.TITLE);
 
var d =new Parent('demo');
d.pubMethod();
Parent.StaticMe();
 

这种形式由于作用域的限制,最好不要实现继承

 

完整的例子包括DOJO jQuey ExtJS MS AJAX的集成包例子可在如下地址下载:

http://jsfkit.codeplex.com/Project/Download/FileDownload.aspx?DownloadId=153492
http://jsfkit.codeplex.com/releases/53146/download/153653

posted @   2012  阅读(440)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示