7.11心得

var decimalDigits = 2,
tax = 5;

function add(x, y) {
return x + y;
}

function subtract(x, y) {
return x - y;
}

//alert(add(1, 3));
但是,这个并不能体现OOP思想,看了原型与原型链之后觉得OOP一目了然:

var Calculator = function (decimalDigits, tax) {
this.decimalDigits = decimalDigits;
this.tax = tax;
};
然后给Calculator的prototype属性赋值对象字面量来设定Calculator对象的原型。(个人觉得这里的原型就如同C#中类的概念,prototype则是用来给类添加属性,方法的)

复制代码
Calculator.prototype = {
add: function (x, y) {
return x + y;
},

subtract: function (x, y) {
return x - y;
}
};
//alert((new Calculator()).add(1, 3));
这样,通过new 一个对象就可以调用里面的公开的方法,属性。

posted on 2017-07-12 09:44  时光在飞逝  阅读(85)  评论(0编辑  收藏  举报