javascript prototype应用

来源:互联网 发布:js弹出模态框 编辑:程序博客网 时间:2024/04/30 01:57

/*-------------------------- 单个 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应用简单实例代码:


0 0
原创粉丝点击