zoukankan      html  css  js  c++  java
  • JavaScript Tutorial 05 #Class#

    /* range.js */
    function range(from, to) {
        var r = inherit(range.methods);
        r.from = from;
        r.to = to;
        return r;
    }
    
    range.methods = {
        includes: function(x) {
            return this.from <= x && x <= this.to;
        },
        foreach: function(f) {
            for (var x=Math.ceil(this.from); x<=this.to; x++) f(x);
        },
        toString: function() {
            return "(" + this.from + "..." + this.to + ")";
        }
    };
    
    //普通函数首字母小写, 定义类首字母大写
    function Range(from, to) {
        this.from = from;
        this.to = to;
    }
    Range.prototype = {
    
    };
    
    /* Complex.js */
    function Complex(real,imgainary) {
        if(isNaN(real) || isNaN(imgainary)) {
            throw new TypeError();
        }
        this.r = real;
        this.i = imgainary;
    }
    
    Complex.prototype.add = function(that) {
        return new Complex(this.r + that.r, this.i + that.i);
    };
    /* 类字段 */
    Complex.parse = function(s) {
        try {
            var m = Complex._format.exec(s);
            return new Complex(parseFloat(m[1]), parseFloat(m[2]));
        } catch(x) {
            throw new TypeError("Can't parse '" + s + "' as a complex number.");
        }
    };
    Complex._format = /^{([^,]+),([^}]+)}$/;

  • 相关阅读:
    CSS选择器
    CSS框模型
    AJAX
    HTML 表单
    二叉树
    词嵌入、word2vec
    双向、深层循环神经网络
    20201012----每天都在进步吗?
    20201012--环境搭建
    20201011--记录一下
  • 原文地址:https://www.cnblogs.com/lambdatea/p/3377939.html
Copyright © 2011-2022 走看看