zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Codeforces 590 A:Median Smoothing
    HDU 1024:Max Sum Plus Plus 经典动态规划之最大M子段和
    POJ 1027:The Same Game 较(chao)为(ji)复(ma)杂(fan)的模拟
    【算法学习】 在一天的24小时之中,时钟的时针、分针和秒针完全重合在一起的时候有几次?
    【读书笔记】 spinlock, mutex and rwlock 的性能比较
    【读书笔记】 nginx 负载均衡测试
    【读书笔记】 多线程程序常见bug
    关注一下 hurd OS的开发
    【读书笔记】 分布式文件存储系统 MogileFS
    【读书笔记】 nginx + memcached 高速缓存
  • 原文地址:https://www.cnblogs.com/2018/p/1838811.html
Copyright © 2011-2022 走看看