English 中文(简体)
建立/普及 Java 习俗
原标题:Creating/populating Javascript custom object

I ve创建了一个灰页,将为我提供一部全面的用户信息的XML文件。 我不敢肯定,这是创造和充实我的习惯 j器的最佳途径。 我以两种方式看待这些问题:

function User() {
   this.Id;
   this.FirstName;
   this.LastName;
   this.Title;
}

以及

var User2 = {
   Id: null,
   FirstName: null,
   LastName: null,
   Title: null
}

我可以通过做如下事情来充实其中的每一部分:

//first object
User.Id = 1

//second object
User2.FirstName =  John 

造物的方法是否优于其他方法?

Edit: A year 以及 a half later I saw that this question got the popular badge, so I just wanted to mention that today I am using Crockford s module pattern.

最佳回答

如果没有即使用<条码>新,你就无法进入第一物体的特性。 关键词:

 var myUser = new User() ;
 document.write(myUser.id) ;

第二件物体是一字不提的物体,因为一栏已经明了。

如果你想要使用<条码>prototypical Legacy<>/code>来根据旧的一条新物体创建新物体,那么这种区别就会发生。 撰写物体字面可能更容易理解,如果你有相当严格的密码基础,则更合适的方式。 然而,如果你想通过增加现有物体和另一个物体,而不必重写已扩大的物体,从而制造新的物体,那么就会产生 hand忙:

ipUser.prototype = User ;
ipUser.ip =  "128.0.0.1" ;

在你看来,这种区别可能并不明显,但你必须想象,如果你会给原物体的每增加一个字面上的物体,你会获得多少多余的代码。

。 2008年7月1日,日内瓦 如你有其他问题,则概述如下::https://developer.mozilla.org/en/Core_Java_1.5_Reference:Global_Objects:Object

Hth

问题回答

你们不要首先制造一个具有空洞价值的物体。 Java本不需要拥有者,可以在任何时候积极增加财产和方法。 这也是:

var User2 = {};
User2.Id = 1;
User2.FirstName =  John ;
//...Etc.

如果你只是对数据储存感到关切,那么我就使用这一表格(目标字面,即你的第二个方法)。

更新:你还可以使事情更加容易,并创造功能,为你们制造用户物体:

function createUser(id, firstname, lastname, title) {
    return {
        Id: id,
        FirstName: firstname,
        LastName: lastname,
        Title: title
    };
}
var User2 = createUser(1,  John ,  Smith ,  Manager );

第一个是奥佩组织的典型目标。 您可以增加职能,根据这种特性采取行动(假设有这样的功能):

var u = new User();
u.getFullName();

第二个方面只是一个联系阵列,其中的扼杀被描绘成价值观。

在 Java,两者之间的边界并不严格,如oh式方案规划语言。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.