Correction status:qualified
Teacher's comments:你的案例不错, 很形象
构造函数,说通俗一点就是建立一个大商场,有摊位卖商品,其他人可以进商场在这个摊位上卖,也可以自己摆摊卖,还可以自己摆摊拿其他摊位上的商品卖!卖的商品就是属性,摊位就是函数
代码实例如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>构造函数</title> </head> <body> <script> var CreatObj = function () { this.itemnumber = 8; this.price = 20 ; this.money = function (itemnumber,price) { return itemnumber * price ; }; }; var obj1 = new CreatObj(); console.log(obj1.itemnumber); console.log(obj1.price); console.log(obj1.money(obj1.itemnumber , obj1.price)); CreatObj.prototype.price2 = 50 ; CreatObj.prototype.money2 = function (itemnuber,price2) { return itemnuber*price2 ; }; var obj2 = new CreatObj(); console.log(obj2.price); console.log(obj2.price2); console.log(obj2.itemnumber); console.log(obj2.money(obj2.itemnumber , obj2.price)); console.log(obj2.money(obj2.itemnumber , obj2.price2)); console.log(obj2.money2(obj2.itemnumber , obj2.price)); console.log(obj2.money2(obj2.itemnumber , obj2.price2)); </script> </body> </html>
点击 "运行实例" 按钮查看在线实例