美文网首页
字面量、工厂方法、构造函数创建对象(加、减、乘、除、平方根方法)

字面量、工厂方法、构造函数创建对象(加、减、乘、除、平方根方法)

作者: 阿迪呀dity | 来源:发表于2017-04-19 20:36 被阅读0次

    字面量创建对象(加)

    var sumObj = {

            num1: 8,

            num2: 16,

            sumFn: function(){

                      return sumObj.num1 + sumObj.num2;

            }

    }

    alert(sumObj.sumFn());

    字面量创建对象(减)

    var minus = {

           num1: 24,

           num2: 12,

            getMinus: function(){

                   alert(minus.num1-minus.num2);

            }

    };

    minus.getMinus();

    字面量创建对象(乘)

    var mult = {

           num1: 5,

           num2: 5,

           getMult: function(){

                   alert(mult.num1 * mult.num2);

          }

    };

    mult.getMult();

    字面量创建对象(除)

    var Divi = {

            num1: 25,

            num2: 5,

             getDivi: function(){

                   alert(Divi.num1/Divi.num2);

              }

    };

    Divi.getDivi();

    字面量创建对象(平方根)

    var Root = {

               num: 5,

              getRoot: function(){

                        alert(Root.num*Root.num);

                }

    };

    Root.getRoot();

    //工厂方法创建对象(加)

    function sumFn(num1,num2){

                  var sum = {};

                 sum.num1 = num1;

                  sum.num2 = num2;

                  sum.getSum = function(){

                             return num1 + num2;

                  }

    return sum;

    }

    var sum1 = sumFn(12,12);

    alert(sum1.getSum());

    工厂方法创建对象(减)

    function minusFn(num1,num2){

                 var minus = {};

                 minus.num1 = num1;

                  minus.num2 = num2;

                 minus.getMinus = function (){

                            alert(num1 - num2);

                  }

    return minus;

    }

    var minus1 = minusFn(24,12);

    minus1.getMinus();

    工厂方法创建对象(乘)

    function multFn(num1,num2){

                var mult = {};

                mult.num1 = num1;

                mult.num2 = num2;

                mult.getMult = function(){

                          alert(num1*num2);

    }

    return mult;

    }

    var mult1 = multFn(5,5);

    mult1.getMult();

    工厂方法创建对象(除)

    function DiviFn(num1,num2){

                var divi = {};

               divi.num1 = num1;

                divi.num2 = num2;

                divi.getDivi = function(){

                         alert(num1/num2);

    }

    return divi;

    }

    var divi1 = DiviFn(25,5);

    divi1.getDivi();

    工厂方法创建对象(平方根)

    function RootFn(num){

                var root = {};

                  root.num = num;

                root.getRoot = function(){

                      alert(num*num);

    }

    return root;

    }

    var root1 = RootFn(5);

    root1.getRoot();

    构造函数创建对象(加)

    function SumFn(num1,num2) {

                   this.num1 = num1;

                  this.num2 = num2;

                  this.getSum = function () {

                              return this.num1+this.num2;

                 };

    }

    var sum1 = new SumFn(12,12);

    alert(sum1.getSum());

    构造函数创建对象(减)

    function minusFn(num1,num2){

                  this.num1 = num2;

                  this.num2 = num2;

                 this.getMinus = function(){

                          alert(this.num1-this.num2);

                   };

    }

    var minus = new minusFn(24,12);

    minus.getMinus();

    构造函数创建对象(乘)

    function MultFn(num1,num2){

                 this.num1 = num1;

                  this.num2 = num2;

                this.getMult = function(){

                           alert(this.num1*this.num2);

                  };

    }

    var mult = new MultFn(5,5);

    mult.getMult();

    构造函数创建对象(除)

    function DiviFn(num1,num2){

                this.num1 = num1;

                this.num2 = num2;

                 this.getDivi = function(){

                               alert(this.num1/this.num2);

    };

    }

    var divi = new DiviFn(25,5);

    divi.getDivi();

    构造函数创建对象(平方根)

    function RootFn(num){

                 this.num = num;

                  this.getRoot = function(){

                          alert(num*num);

                 };

    }

    var root = new RootFn(5);

    root.getRoot();

    相关文章

      网友评论

          本文标题:字面量、工厂方法、构造函数创建对象(加、减、乘、除、平方根方法)

          本文链接:https://www.haomeiwen.com/subject/rlwhzttx.html