/    Sign up×
Community /Pin to ProfileBookmark

Proper syntax for adding methods to objects

Hi everyone ?,

I am developing an application used for some registration of data at my workplace, and landed on an AJAX architecture using a series of C# developed services and a “client application” in javascript for user interaction.

I like to work in an object oriented fashion, and have previously defined objects as such:

[CODE]function MyObject()
{
this.myMethod = myMethod;

function myMethod()
{ /* code */ }
}
var obj = new MyObject()[/CODE]

However, this time I tried to simplify my code by doing:

[CODE]function MyObject()
{
this.myMethod = function myMethod()
{ /* code */}
}
var obj = new MyObject()[/CODE]

This works well, and browsers have no problems with this. However, Netbeans does not manage to derive code completion suggestions from this code. It simply does not recognize [I]myMethod[/I] as methods inside the [I]MyObject[/I] object.

Is this indicative that it is not correct somehow to assign function pointers the way I do?

It is not major issue, but it does make my code clean, IMHO.

Best, Knut

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jul 13.2009 — No, that's absolutely fine, and preferable in my opinion. JavaScript treats functions like variables (very like LISP), which is why you can pass functions around like variables.
×

Success!

Help @Knutsi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...