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 new 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:
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
}
You can then call this function when you declare the "array," as shown in this example;
var code_listings = new create_array(16)
What? return this? Why in the world would you return this?
Another example of this can be seen in this so-called "custom object front-end":
function custom_object(actual_object) {
this.actual_object = actual_object
this.move = move_object
}
It then gives an explanation of how browser A, browser B, and other browsers use their own move() method (declared in some earlier script). Oh, yeah, and the object you are trying to move has been declared as current_object. :
function move_object(how_much) {
if (browser A) {
this.actual_object.moveA(how_much)
}
else if (browser B) {
this.actual_object.moveB(how_much)
}
else {
this.actual_object.moveOther(how_much)
}
}
Call the constructor function to create the custom object that you'll use as a front-end:
my_object = new custom_object(current_object)
Once you do that, you can then apply the custom object's properties and methods to the object you created:
my_object.move(10)
Alright, I know it's a lot to read, but how exactly do I utilize creating objects using the new keyword -- how does it work, and how can I make them use methods that take arguments?
Creating your own Array object:
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
}
You can then call this function when you declare the "array," as shown in this example;
var code_listings = new create_array(16)
What? return this? Why in the world would you return this?
Another example of this can be seen in this so-called "custom object front-end":
function custom_object(actual_object) {
this.actual_object = actual_object
this.move = move_object
}
It then gives an explanation of how browser A, browser B, and other browsers use their own move() method (declared in some earlier script). Oh, yeah, and the object you are trying to move has been declared as current_object. :
function move_object(how_much) {
if (browser A) {
this.actual_object.moveA(how_much)
}
else if (browser B) {
this.actual_object.moveB(how_much)
}
else {
this.actual_object.moveOther(how_much)
}
}
Call the constructor function to create the custom object that you'll use as a front-end:
my_object = new custom_object(current_object)
Once you do that, you can then apply the custom object's properties and methods to the object you created:
my_object.move(10)
Alright, I know it's a lot to read, but how exactly do I utilize creating objects using the new keyword -- how does it work, and how can I make them use methods that take arguments?