文档库 最新最全的文档下载
当前位置:文档库 › JavaScript 攻略(对象)

JavaScript 攻略(对象)

?JavaScript对象

JavaScript 中的所有事物都是对象:字符串、数值、数组、函数...
此外,JavaScript 允许自定义对象。
JavaScript 对象
JavaScript 提供多个内建对象,比如 String、Date、Array 等等。
对象只是带有属性和方法的特殊数据类型。
访问对象的属性
属性是与对象相关的值。
访问对象属性的语法是:
objectName.propertyName
这个例子使用了 String 对象的 length 属性来获得字符串的长度:
var message="Hello World!";
var x=message.length;
在以上代码执行后,x 的值将是:
12
访问对象的方法
方法是能够在对象上执行的动作。
您可以通过以下语法来调用方法:
objectName.methodName()
这个例子使用了 String 对象的 toUpperCase() 方法来将文本转换为大写:
var message="Hello world!";
var x=message.toUpperCase();
在以上代码执行后,x 的值将是:
HELLO WORLD!
创建 JavaScript 对象
通过 JavaScript,您能够定义并创建自己的对象。
创建新对象有两种不同的方法:
定义并创建对象的实例
使用函数来定义对象,然后创建新的对象实例
创建直接的实例
这个例子创建了对象的一个新实例,并向其添加了四个属性:
实例
person=new Object();
person.firstname="Bill";
https://www.wendangku.net/doc/2a13826797.html,stname="Gates";
person.age=56;
person.eyecolor="blue";
替代语法(使用对象 literals):
实例
person={firstname:"John",lastname:"Doe",age:50,eyecolor:"blue"};
使用对象构造器
本例使用函数来构造对象:
实例
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
https://www.wendangku.net/doc/2a13826797.html,stname=lastname;
this.age=age;
this.eyecolor=eyecolor;
}
创建 JavaScript 对象实例
一旦您有了对象构造器,就可以创建新的对象实例,就像这样:
var myFather=new person("Bill","Gates",56,"blue");
var myMother=new person("Steve","Jobs",48,"green");
把属性添加到 JavaScript 对象
您可以通过为对象赋值,向已有对象添加新属性:
假设 personObj 已存在 - 您可以为其添加这些新属性:firstname、lastname、age 以及 eyecolor:
person.firstname="Bill";
https://www.wendangku.net/doc/2a13826797.html,stname="Gates";
person.age=56;
person.eyecolor="blue";
x=person.firstname;
在以上代码执行后,x 的值将是:
Bill
把方法添加到 JavaScript 对象
方法只不过是附加在对象上的函数。
在构造器函数内部定义对象的方法:
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
https://www.wendangku.net/doc/2a13826797.html,stname=lastname;
this.age=age;
this.eyecolor=eyecolor;
this.changeName=changeName;
function changeName(name)
{
https://www.wendangku.net/doc/2a13826797.html,stname=name;
}
}
changeName() 函数 name 的值赋给 person 的 lastname 属性。
现在您可以试一下:
myMother.changeName("Ballmer");
JavaScript 类
JavaScript 是面向对象的语言,但 JavaScript 不使用类。
在 JavaScript 中,不会创建类

,也不会通过类来创建对象(就像在其他面向对象的语言中那样)。
JavaScript 基于 prototype,而不是基于类的。
JavaScript for...in 循环
JavaScript for...in 语句循环遍历对象的属性。
语法
for (对象中的变量)
{
要执行的代码
}
注释:for...in 循环中的代码块将针对每个属性执行一次。
实例
循环遍历对象的属性:
var person={fname:"Bill",lname:"Gates",age:56};
for (x in person)
{
txt=txt + person[x];
}
附C 源码:
using System;
using System.Data;
using System.Collections.Generic;
using https://www.wendangku.net/doc/2a13826797.html,mon;
using Maticsoft.Model;
namespace Maticsoft.BLL
{
///


/// Cuser
///

public partial class Cuser
{
private readonly Maticsoft.DAL.Cuser dal=new Maticsoft.DAL.Cuser();
public Cuser()
{}
#region BasicMethod
///
/// 是否存在该记录
///

public bool Exists(string UserID)
{
return dal.Exists(UserID);
}

///
/// 增加一条数据
///

public bool Add(Maticsoft.Model.Cuser model)
{
return dal.Add(model);
}

///
/// 更新一条数据
///

public bool Update(Maticsoft.Model.Cuser model)
{
return dal.Update(model);
}

///
/// 删除一条数据
///

public bool Delete(string UserID)
{

return dal.Delete(UserID);
}
///
/// 删除一条数据
///

public bool DeleteList(string UserIDlist )
{
return dal.DeleteList(UserIDlist );
}

///
/// 得到一个对象实体
///

public Maticsoft.Model.Cuser GetModel(string UserID)
{

return dal.GetModel(UserID);
}

///
/// 得到一个对象实体,从缓存中
///

public Maticsoft.Model.Cuser GetModelByCache(string UserID)
{

string CacheKey = "CuserModel-" + UserID;
object objModel = https://www.wendangku.net/doc/2a13826797.html,mon.DataCache.GetCache(CacheKey);
if (objModel == null)
{
try
{
objModel = dal.GetModel(UserID);
if (objModel != null)
{
int ModelCache = https://www.wendangku.net/doc/2a13826797.html,mon.ConfigHelper.GetConfigInt("ModelCache");
https://www.wendangku.net/doc/2a13826797.html,mon.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
}
}
catch{}
}
return (Maticsoft.Model.Cuser)objModel;
}

///
/// 获得数据列表
///

public DataSet GetList(string strWhere)
{
return dal.GetList(strWhere);
}
///
/// 获得前几行数据
///

public DataSet GetList(int Top,string strWhere,string filedOrder)
{
return dal.GetList(Top,strWhere,filedOrder);
}
///
/// 获得数据列表
///

public List GetModelList(string strWhere)
{
DataSet ds = dal.GetList(strWhere);
return D

ataTableToList(ds.Tables[0]);
}
///


/// 获得数据列表
///

public List DataTableToList(DataTable dt)
{
List modelList = new List();
int rowsCount = dt.Rows.Count;
if (rowsCount > 0)
{
Maticsoft.Model.Cuser model;
for (int n = 0; n < rowsCount; n++)
{
model = dal.DataRowToModel(dt.Rows[n]);
if (model != null)
{
modelList.Add(model);
}
}
}
return modelList;
}

///
/// 获得数据列表
///

public DataSet GetAllList()
{
return GetList("");
}

///
/// 分页获取数据列表
///

public int GetRecordCount(string strWhere)
{
return dal.GetRecordCount(strWhere);
}
///
/// 分页获取数据列表
///

public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
{
return dal.GetListByPage( strWhere, orderby, startIndex, endIndex);
}
///
/// 分页获取数据列表
///

//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
//{
//return dal.GetList(PageSize,PageIndex,strWhere);
//}

#endregion BasicMethod
#region ExtensionMethod

#endregion ExtensionMethod
}
}

//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GZGLXT.h"
#include "ADOConn.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ADOConn::ADOConn()
{

}

ADOConn::~ADOConn()
{

}

void ADOConn::OnInitADOConn()
{
::CoInitialize(NULL);
try
{
m_pConnection.CreateInstance("ADODB.Connection");
_bstr_t strConnect="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=GZFFXT;Data Source=.";
m_pConnection->Open(strConnect,"","",adModeUnknown);
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}
}

_RecordsetPtr& ADOConn::GetRecordSet(_bstr_t bstrSQL)
{
try
{
if(m_pConnection==NULL)
OnInitADOConn();
m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);
}
catch(_com_error e)
{
e.Description();
}
return m_pRecordset;
}

BOOL ADOConn::ExecuteSQL(_bstr_t bstrSQL)
{
_variant_t RecordsAffected;
try
{
if(m_pConnection==NULL)
OnInitADOConn();
m_pConnection->Execute(bstrSQL,NULL,adCmdText);
return true;
}
catch(_com_error e)
{
e.Description();
return false;
}
}

void ADOConn::ExitConnect()
{
if(m_pRecordset!=NULL)

m_pRecordset->Close();
m_pConnection->Close();
::CoUninitialize();
}

相关文档
相关文档 最新文档