Advertisement
Advertisement

新足迹

 找回密码
 注册
新足迹 门户 IT专业论坛 查看内容

Javascript Object Inheritance-Code reuse ---For fun

2011-2-9 11:04| 发布者: | 查看: 1104| 原文链接

Take a real scenario
With a javascript application to deal with transactions.
We can design a general object called "transaction", as parent object
Then design some other objects (invoice / stockreturn/ stockswap/stockadjust...), as child objects

parent object
var transaction={
        createdon:XXX,
        createdby:"xxxx",
        totalamount:"xxx"
        cal_gst:function(rate){
                this.totalamount*rate
        }
};

Child object
var invoice=inheritfrom(transaction)
invoice.setinvoiceno=function(){
        //some codes
        }

Then the invoice object has all the members as transaction has.

var stockreturn=inheritfrom(transaction)
stockreturn.stock_reusable=false...
Then the stockreturn object has all the members as transaction has.
Advertisement
Advertisement


Advertisement
Advertisement
返回顶部