javascript设计模式——第一章 富有表现力的javascript

1.1 javascript的灵活性
        //1.面向过程编程,无法创建可以保存状态并且具有一些仅对其内部状态进行操作的方法的动画对象。
        function startAnimation() {
 
        }
 
        function stopAnimation() {
 
        }
 
        //2.定义一个类
        var Animal = function () { };
        Animal.prototype.start = function () { };
        Animal.prototype.stop = function () { };
 
        /*使用*/
        var a = new Animal();
        a.start();
        a.stop();
 
 
        //3.对第2种写法的封装
        var Animal = function () { };
        Animal.prototype = {
            start: function () { },
            stop: function () { }
        };
 
        //4.通过一个方法为类添加方法属性
        Function.prototype.addMethod = function (propertyName, fn) {
            this.prototype[propertyName] = fn;
            return this;
        };
 
        //使用
        function Animal() { }
        Animal.addMethod('start', function () { });
        Animal.addMethod('stop', function () { });
 
        //5.链式编程
        Animal.addMethod('start', function () { }).addMethod('stop', function () { });
 
1.2 弱类型语言
3种原始类型:布尔型、数值型和字符串类型
对象类型和(包含可执行代码的)函数类型
null(空类型)和未定义类型(undefined)
 
原始类型按值进行传送,其他数据类型则按引用传送
原始类型之间可以进行类型转换:toString()可以把数值和布尔值转变为字符串;
parseFloat()和parseInt()可以把字符串转换为数值
!!可以把字符串转变为布尔值。
 
1.3 函数是一等对象
        (function () {
            var a = 9, b = 10;
            alert(a * b);
        })();
 
        //等价
        (function (a, b) {
            alert(a * b);
        })(2, 3);
 
匿名函数最有价值的用途是用来创建闭包。闭包是一个受到保护的变量空间,由内嵌函数生成。
        //闭包
        var res;
        (function () {
            var a = 22, b = 33;
            res = function () {
                alert(a * b);
            }
        })();
 
        res();
变量a和b定义在匿名函数中,因为函数res定义在这个闭包中,所以它也能访问这两个变量,即使在该闭包执行结束后。
 
1.4 对象的易变性
        function Person(name, age) {
            this.name = name;
            this.age = age;
        }
 
        Person.prototype = {
            getName: function () {
                return this.name;
            },
            getAge: function () {
                return this.age;
            }
        }
 
        var p1 = new Person('张三', 23);
        var p2 = new Person('李四', 45);
 
        Person.prototype.getGreeting = function () {
            return 'hi' + this.getName() + '!';
        };
 
        //仅p2实例拥有的方法,其它实例没有这个方法!!
        p2.displayGreeting = function () {
            alert(this.getGreeting());
        };
 
        p2.displayGreeting();
 
 
        //内省,运行代码时检查对象所具有的属性和方法,使用这些信息动态实例化类和执行其方法
        //这种技术称为反射!不需要在开发时知道它们的名称
        var p3 = new Person('王五', 100);
        if (p3.displayGreeting && typeof p3.displayGreeting=='function') {
            p3.displayGreeting();
        } else {
            var res = p3.getGreeting();
            alert(res);
        }
 
 
1.5 继承
原型式继承
classA.prototype=classB.prototype;
 
类式继承
function classA(){
    b.call(this,arguments);
}
 
for(var k in classB.prototype){
    classA.prototype[k]=classB.prototype[k];
}
 
function classB(){
 
}
 
1.6 javascript中的设计模式
3个原因使用设计模式
1> 可维护性。降低模块间的耦合程度
2> 沟通。一个名词可以概括一大堆代码
3>性能。某些模式是起优化作用的模式。
 
2个理由不使用设计模式
1>复杂性。把代码变复杂,更难被新手理解。
2>性能。多数模式对代码的性能都有所拖累。
设计模式比较容易,懂得应该什么时候使用什么模式则较为困难。

posted on 2013-09-02 23:37  67老白干  阅读(114)  评论(0)    收藏  举报

导航