栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

javascript工厂方式定义对象

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

javascript工厂方式定义对象

每一个函数对象都有一个length属性,表示该函数期望接收的参数个数。

复制代码 代码如下:







关于js面向对象的创建方式,

目标:

构建一个order对象.
包含三个属性:日期,金额,提交人 
包含一个方法:显示字符串:”XX在XXXX-XX-XX 提交了额度为:XXXX元的订单"

一 工厂方式

复制代码 代码如下:
        

二 构造函数方式

复制代码 代码如下:

function Order()
{
     this.Date = "1990-1-1";
     this.Price = "3200";
     this.Name = "Vince Keny";
     this.Show = function()
         {
              alert(this.Name + " 在 " + this.Date + " 提交了额度为 " + this.Price + " 元的订单.")
         }
}
 
var order = new Order();
order.Show();

三 原型方式

复制代码 代码如下:

function Order()
{}
 
Order.prototype.Date = "1990-1-1";
Order.prototype.Price = "3200";
Order.prototype.Name = "Vince Keny";
Order.prototype.Show = function()
     {
         alert(this.Name + " 在 " + this.Date + " 提交了额度为 " + this.Price + " 元的订单.")
     }
var order = new Order();
order.Show();

四 混合 构造函数/原型 方式

复制代码 代码如下:

function Order()
{
     this.Date = "1990-1-1";
     this.Price = "3200";
     this.Name = "Vince Keny";
}
Order.prototype.Show = function().
{
         alert(this.Name + " 在 " + this.Date + " 提交了额度为 " + this.Price + " 元的订单.")
}
var order = new Order();
order.Show();

五 动态混合方式

复制代码 代码如下:

function Order()
{
     this.Date = "1990-1-1";
     this.Price = "3200";
     this.Name = "Vince Keny";
   
     if(typeof Order._initialized == "undefined")
     {
         Order.prototype.Show = function().
                       {
                            alert(this.Name + " 在 " + this.Date + " 提交了额度为 " + this.Price + " 元的订单.")
                       };
         Order._initialized = true;
     }
}

    function Car(sColor,iDoors){
        var oTempCar = new Object;
        oTempCar.color = sColor;
        oTempCar.doors = iDooes;
        oTempCar.showColor = function (){
            alert(this.color)
        };
        return oTempCar;
    }
    var oCar1 = new Car("red",4);
    var oCar2 = new Car("blue",3);
    oCar1.showColor();        //outputs "red"
    oCar2.showColor();        //outputs "blue"

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/104893.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号