《JavaScript语言精粹》经典记录

书中避开鸡肋与糟粕,只讨论精华部门,不谈DOM与HTML,只关注语言本身。

JavaScript的确是一种非常优雅的语言,直接用代码表示吧。

以下代码摘自《JavaScript语言精粹》,大多表现着一种基于原型的弱类型语言的特性,

经过调试,略有修改。

//给类型加方法
Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this;
};

//实例化一个构造函数
if(typeof Object.beget !=='function'){
    Object.beget=function(o){
        var F=function () {};
        F.prototype=o;
        return new F();
    };
}

//异常处理
try{
    document.writeln(document.body.childNodes);
}
catch(e){
document.writeln(e.name+':'+e.message);    
}

//闭包
var add_the_handlers=function (nodes){
    var i;
    for(i=0;i<nodes.length;i+=1){
        nodes[i].onclick=function (i){
            return function (e){
                alert(i);
            };
        }(i);
    }
};
add_the_handlers(document.body.childNodes);

//模块
String.method('deentityify', function () {
    var entity = {
        quot: '"',
        lt: '<',
        gt: '>'
    };
    return function () {
        return this.replace(/&([^&;]+);/g,
            function (a,b){
                var r=entity[b];
                return typeof r==='string'?r:a;
            }
        );
    };
    } ()
);
document.writeln('<br>');
document.writeln('&lt;&quot;&gt;'.deentityify());
document.writeln('123'.deentityify());

//伪类
Function.method('new', function () {
    var that = Object.beget(this.prototype);
    var other = this.apply(that, arguments);
    return (typeof other === 'object' && other) || that;
});

var Mammal = function (name) {
    this.name = name;
    this.age = 1;
};
Mammal.prototype.get_name = function () {
    return this.name;
};
Mammal.prototype.says = function () {
    return this.saying || '';
};
Mammal.prototype.get_age = function () {
    return this.age;
};
var myMammal = new Mammal('Herb the Mammal');
var name = myMammal.get_name(); //可以访问到name属性
//
var Cat = function (name) {
//
    this.name = name;
//
    this.saying = 'meow';
//
};
//
Cat.prototype = new Mammal();
//
Cat.prototype.purr = function (n) {
//
    var i, s = '';
//
    for (i = 0; i < n; i++) {
//
        if (s) {
//
            s += '-';
//
        }
//
        s += 'r';
//
    }
//
    return s;
//
};
//
Cat.prototype.get_name = function () {
//
    return this.says() + ' ' + this.name + ' ' + this.says();
//
};
//
现在除了特定的外,还是可以访问到私有属性。

//定义inherits方法,以实现继承伪类
Function.method('inherits', function (Parent) {
    this.prototype = new Parent();
    return this;
});
var Cat = function (name) {
    this.name = name;
    this.saying = 'meow';
} .inherits(Mammal).method('purr', function (n) {
    var i, s = '';
    for (i = 0; i < n; i++) {
        if (s) {
            s += '-';
        }
        s += 'r';
    }
    return s;
}).method('get_name', function () {
    return this.says() + ' ' + this.name + ' ' + this.says();
});
var myCat = new Cat('Henrietta');
var says = myCat.says(); //'meow'
var purr = myCat.purr(5); //'r-r-r-r-r'
var name = myCat.get_name(); //'meow Henrietta meow'
var name1 = myCat.name;
//var age1 = mycat.get_age();//无法访问父类方法
document.writeln('<br>');
document.writeln(says + ' ' + purr + ' ' + name + ' '+name1);
 
//维度
//
创建包含10个0的数组
Array.dim=function(dimension,initial){
    var a=[],i;
    for(i=0;i<dimension;i+=1){
        a[i]=initial;
    }
    return a;
}
var myArray=Array.dim(10,0);

//给Array添加矩阵函数
Array.matrix=function(m,n,initial){
    var a,i,j,mat=[];
    for(i=0;i<m;i+=1){
        a=[];
        for(j=0;j<n;j+=1){
            a[j]=initial;
        }
        mat[i]=a;
    }
    return mat;
}
//0填充4*4的矩阵
var myMatrix=Array.matrix(4,4,0);
document.writeln(myMatrix[3][3]);    //0

//用来构造一个恒等矩阵的方法。
Array.identity=function(n){
    var i,mat=Array.matrix(n,n,0);
    for(i=0;i<n;i+=1){
        mat[i][i]=1;
    }
    return mat;
};
myMatrix=Array.identity(4);
document.writeln(myMatrix[3][3]);    //
原文地址:https://www.cnblogs.com/13yan/p/2286282.html