/    Sign up×
Community /Pin to ProfileBookmark

Objects in Javascript

I have this book on JavaScript, and it has many script examples. Some of these depict creating an object (with properties and methods and all that good stuff) using the [B]new[/B] keyword. Unfortunately, it doesn’t explain how creating an object works. For example (I know it’s a lot; just scan it to get a general idea):

Creating your own Array object:

[QUOTE]

[FONT=courier new]function create_array(number_of_values) {
this.length = number_of_values
for (var counter = 0; counter < number_of_values; counter++) {
this[counter] = null

}
return this
}[/FONT]

[FONT=arial]You can then call this function when you declare the “array,” as shown in this example;[/FONT]
[FONT=courier new]
var code_listings = new create_array(16)[/FONT]

[/QUOTE]

What? [I]return this[/I]? Why in the world would you [I]return this[/I]? ?

Another example of this can be seen in this so-called “[I]custom object front-end[/I]“:

[QUOTE]

[FONT=courier new]function custom_object(actual_object) {
this.actual_object = actual_object
this.move = move_object
}
[/FONT]

[/QUOTE]

It then gives an explanation of how browser A, browser B, and other browsers use their own [FONT=courier new]move()[/FONT] method (declared in some earlier script). Oh, yeah, and the object you are trying to move has been declared as [FONT=courier new]current_object[/FONT]. :

[QUOTE]

[FONT=courier new]function move_object(how_much) {
if (browser A) {
this.actual_object.moveA(how_much)
}
else if (browser ? {
this.actual_object.moveB(how_much)
}
else {
this.actual_object.moveOther(how_much)
}

}[/FONT]

Call the constructor function to create the custom object that you’ll use as a front-end:
[FONT=courier new]my_object = new custom_object(current_object)[/FONT]

Once you do that, you can then apply the custom object’s properties and methods to the object you created:

[FONT=courier new]my_object.move(10)[/FONT]

[/QUOTE]

:rolleyes:

Alright, I know it’s a lot to read, but how exactly do I utilize creating objects using the [B]new[/B] keyword — how does it work, and how can I make them use methods that take arguments?

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@MarkEauthorApr 07.2003 — But how does the "this" keyword work in a function and how do its properties work? I don't know how this URL-hash thing works, but how do I use "this" and properties and methods of "this" in a "constructer model"?
×

Success!

Help @MarkE 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.17,
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,
)...