/*-------------------------- 单个 prototype -----------------------------*/
var Calculator = function (x, y) {
this.x = x;
this.y = y;
};
Calculator.prototype.add = function (x, y) {
return x + y;
}
var c = new Calculator();
c.add(3, 7);
/*---------------- js prototype private 类型 -----------------*/
var Calculator = function (x, y) {
this.x = x;
this.y = y;
};
Calculator.prototype = function () {
add = function (x, y) {
return x + y;
},
subtract = function (x, y) {
return x - y;
}
return {
A: add,
S: subtract
}
}; //需添加自执行!
var c = new Calculator();
c.A(3, 7);
以上代码为javascript prototype应用简单实例代码: