码迷,mamicode.com
首页 > Web开发 > 详细

一切皆对象之两个方法概括js,无函数签名(无多态),原型,闭包,封装,引用类型,继承……

时间:2014-09-13 03:00:54      阅读:348      评论:0      收藏:0      [点我收藏+]

标签:javascript   继承   闭包   原型   对象   

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<script type="text/javascript">

    var num = [88, 1151, 1];
    (function() {
        /**
         * 对于n1,n2,n3;如果其中任意两个分别乘以[1, 100]范围内的任意两个整数x,y的和加2或者减2等于
         * 另外第三个数,找出x和y.
         * @type {Number}
         */
        function MioFun () {
            this.a = arguments[0][0];
            this.b = arguments[0][1];
            this.c = arguments[0][2];
        }
        MioFun.prototype = {
            constructor : MioFun,
            findMax: function() {
                var temp = 0;
                if(this.a < this.b) {
                    temp = this.a;
                    this.a = this.b;
                    this.b = temp;
                }
                if(this.a < this.c) {
                    temp = this.a;
                    this.a = this.c;
                    this.c = temp;
                }
                return this;
            },
            calMain: function() {
                var i, j;
                if(this.a < this.b + this.c) return null;
                for(i = 1; i <= 100; i++) {
                    for(j = 1; j <= 100; j++) {
                        this.calFun(i, j);
                    }
                }
            },
            calFun: function(x, y) {
                if((this.a-2 == this.b*x + this.c*y)||(this.a+2 == this.b*x + this.c*y)) {
                    console.log(x, y, this.b + "*" + x + "+" + this.c + "*" + y + "=" + (this.a - 2) + "or" + (this.a + 2));
                    console.log("////////////////////////////////////////////////////////////////////////////");
                }            
            }
        }
        var mio = new MioFun(arguments[0]);
        mio.findMax().calMain();        
    })(num);







    var arg = [88, 1152, 102];
    (function() {
        /**
         * 对于n1,n2,n3;如果其中任意两个分别乘以[1, 100]范围内的任意两个整数x,y的和加2或者减2等于
         * 另外第三个数,找出x和y.
         * @type {Number}
         */
        function SuperFun () { //父类成员属性
            if(arguments[0]) {
                this.a = arguments[0][0];
                this.b = arguments[0][1];
            }
        }
        SuperFun.prototype.findMax = function() { //父类成员方法findMax
            var temp = 0;
            if(this.a < this.b) {
                temp = this.a;
                this.a = this.b;
                this.b = temp;
            }
            if(this.a < this.c) {
                temp = this.a;
                this.a = this.c;
                this.c = temp;
            }
            return this;
        };
        SuperFun.prototype.calFun = function(x, y) { //父类成员方法calFun
            if((this.a-2 == this.b*x + this.c*y)||(this.a+2 == this.b*x + this.c*y)) {
                console.log(x, y, this.b + "*" + x + "+" + this.c + "*" + y + "=" + (this.a - 2) + "or" + (this.a + 2));
                console.log("////////////////////////////////////////////////////////////////////////////");
            }            
        };
        function SubFun () {
            SuperFun.call(this, arguments[0]); //继承父类SuperFun属性
            this.c = arguments[0][2]; //子类伪私有成员属性
        }
        SubFun.prototype = new SuperFun();  //继承父类SuperFun方法
        SubFun.prototype.constructor = SubFun;
        SubFun.prototype.calMain =  function() { //子类伪私有成员方法
            var i, j;
            if(this.a < this.b + this.c) return null;
            for(i = 1; i <= 100; i++) {
                for(j = 1; j <= 100; j++) {
                    this.calFun(i, j);
                }
            }
        };       
        var mio = new SubFun(arguments[0]);
        mio.findMax().calMain();        
    })(arg);
	</script>
</head>
<body>
	
</body>
</html>

bubuko.com,布布扣

一切皆对象之两个方法概括js,无函数签名(无多态),原型,闭包,封装,引用类型,继承……

标签:javascript   继承   闭包   原型   对象   

原文地址:http://blog.csdn.net/xizai2012/article/details/39244265

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!