现在的位置: 首页 > 综合 > 正文

JavaScript基础 实例和框架集成

2013年04月30日 ⁄ 综合 ⁄ 共 2150字 ⁄ 字号 评论关闭

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

//父类示例
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

抱歉!评论已关闭.