扩展JS

//JS的扩展方法: 1 定义类静态方法扩展 2 定义类对象方法扩展
            var aClass = function(){}


            //1 定义这个类的静态方法
            aClass.sayHello = function(){
                alert('say hello');
            }


            //2 定义这个类对象的对象方法
            aClass.prototype.protoSayHello = function(){
                alert('prototype say hello');
            }




            aClass.sayHello() ;//aClass的静态方法


            var aObject = new aClass();
            aObject.protoSayHello();  //类对象的方法扩展


            //JQuery的方法扩展


            //定义jquery的扩展方法
            //1 定义JQuery的静态方法
            jQuery.extend({
                staticHello: function () {
                    alert("wwg, staticHello");
                }
            });


            var str = $.staticHello();


            //2 定义JQuery对象的扩展方法
            jQuery.fn.ObjHello = function () {
                return alert("ObjHello");
            }


            $("#htmlDiv").ObjHello();

---------------------------------------------------------------------------------

<script type="text/javascript">

        //自定义的函数(类)
        function selfFun(name) {
            this.m_Name = name;
        }
        // 为自定义类添加一个扩展一个方法
        selfFun.prototype.Hello = function() {
            document.write("你好!" + this.m_Name);
        };
        var self = new selfFun("张占岭")//为类通过它的构架方法赋值
        self.Hello();
    </script>

<script type="text/javascript">
        //通过函数(类)的prototype属性来继承另一个函数
        function A() {
            this.MethodA = function() {
                document.write("MethodA");
            }
        }
 
        function B() {
            this.MethodB = function() {
                document.write("MethodB");
            }
        }
 
        B.prototype = new A(); //TestObjectB继承了TestObjectA类
        var testObjectB = new B();
        testObjectB.MethodA();
    </script>
*********************************************************************************
<script type="text/javascript">
 
        // 注意,prototype只对本script段起作用
        // prototype原型关键字,为一个JS原对象扩展一个方法
        Array.prototype.max = function() {
            var i, min = this[0];
            for (i = 1; i < this.length; i++) {
                if (min > this[i])
                    min = this[i];
            }
            return min;
        };
 
        var myArray = new Array();
        myArray[0] = 1;
        myArray[1] = 3;
        myArray[2] = 2;
        document.write(myArray.min());
    </script>

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

   4、组合构造函数及原型模式

      目前最为常用的定义类型方式,是组合构造函数模式与原型模式。构造函数模式用于定义实例的属性,而原型模式用于定义方法和共享的属性。结果,每个实例都会有自己的一份实例属性的副本,但同时又共享着对方方法的引用,最大限度的节约内存。此外,组合模式还支持向构造函数传递参数,可谓是集两家之所长。

function Person(name, age, job) {
    this.name = name;
    this.age = age;
    this.job = job;
    this.lessons = ['Math', 'Physics'];
}
Person.prototype = {
    constructor: Person,//原型字面量方式会将对象的constructor变为Object,此外强制指回Person
    getName: function () {
        return this.name;
    }
}
var person1 = new Person('Jack', 19, 'SoftWare Engneer');
person1.lessons.push('Biology');
var person2 = new Person('Lily', 39, 'Mechanical Engneer');
alert(person1.lessons);//Math,Physics,Biology
alert(person2.lessons);//Math,Physics
alert(person1.getName === person2.getName);//true,//共享原型中定义方法

   在所接触的JS库中,jQuery类型的封装就是使用组合模式来实例的!!!

 
    5、动态原型模式

     组合模式中实例属性与共享方法(由原型定义)是分离的,这与纯面向对象语言不太一致;动态原型模式将所有构造信息都封装在构造函数中,又保持了组合的优点。其原理就是通过判断构造函数的原型中是否已经定义了共享的方法或属性,如果没有则定义,否则不再执行定义过程。该方式只原型上方法或属性只定义一次,且将所有构造过程都封装在构造函数中,对原型所做的修改能立即体现所有实例中:

function Person(name, age, job) {
    this.name = name;
    this.age = age;
    this.job = job;
    this.lessons = ['Math', 'Physics'];

}
if (typeof this.getName != 'function') {//通过判断实例封装
  Person.prototype = {
    constructor: Person,//原型字面量方式会将对象的constructor变为Object,此外强制指回Person
    getName: function () {
      return this.name;
    }
  }
}
var person1 = new Person('Jack', 19, 'SoftWare Engneer');
person1.lessons.push('Biology');
var person2 = new Person('Lily', 39, 'Mechanical Engneer');
alert(person1.lessons);//Math,Physics,Biology
alert(person2.lessons);//Math,Physics
alert(person1.getName === person2.getName);//true,//共享原型中定义方法

     注:以上内容参考《JavaScript 高级程序设计》第3版

原文地址:https://www.cnblogs.com/xiangxiong/p/7206476.html