objectName.propertyName
myCar.make = "Ford"
myCar.model = "Mustang"
myCar.year = 69;
myCar["make"] = "Ford"
myCar["model"] = "Mustang"
myCar["year"] = 67
function show_props(obj, obj_name) {
var result = ""
for (var i in obj)
result += obj_name + "." + i + " = " + obj[i] + "\n"
return result
}
So, the function call show_props(myCar,
"myCar") would return the following:
myCar.make = Ford
myCar.model = Mustang
myCar.year = 67
A function definition consists of the function keyword, followed by
For example, here is the definition of a simple function named pretty_print:
function pretty_print(str) {
document.write("<HR><P>" + str)
}
In addition to defining functions as described here, you can also define Function objects, as described in "Function object".
In a Navigator application, you can use (or call) any function defined in the current page. You can also use functions defined by other named windows or frames; for more information, see Chapter 3, "Using windows and frames." In a LiveWire application, you can use any function compiled with the application.
<SCRIPT>
pretty_print("This is some text to display")
</SCRIPT>
The arguments of a function are not limited to strings and numbers. You can pass whole objects to a function, too. The show_props function (defined in "Objects and properties") is an example of a function that takes an object as an argument.
function factorial(n) {
if ((n == 0) || (n == 1))
return 1
else {
result = (n * factorial(n-1) )
return result
}
}
You could then display the factorials of one through five as follows:
for (x = 0; x < 5; x++) {
document.write("<BR>", x, " factorial is ", factorial(x))
}
0 factorial is 1
1 factorial is 1
2 factorial is 2
3 factorial is 6
4 factorial is 24
5 factorial is 120
functionName.arguments[i]
function list(type) {
document.write("<" + type + "L>") // begin list
// iterate through arguments
for (var i = 1; i < list.arguments.length; i++)
document.write("<LI>" + list.arguments[i])
document.write("</" + type + "L>") // end list
}
list("o", "one", 1967, "three", "etc., etc...")
1.one
2.1967
3.three
4.etc., etc...
function car(make, model, year) {
this.make = make
this.model = model
this.year = year
}
Now you can create an object called mycar as follows:
mycar = new car("Eagle", "Talon TSi", 1993)
You can create any number of car objects by calls to new. For example,
kenscar = new car("Nissan", "300ZX", 1992)
function person(name, age, sex) {
this.name = name
this.age = age
this.sex = sex
}
and then instantiate two new person objects as follows:
rand = new person("Rand McKinnon", 33, "M")
ken = new person("Ken Jones", 39, "M")
function car(make, model, year, owner) {
this.make = make
this.model = model
this.year = year
this.owner = owner
}
To instantiate the new objects, you then use the following:
car1 = new car("Eagle", "Talon TSi", 1993, rand)
car2 = new car("Nissan", "300ZX", 1992, ken)
car2.owner.name
Note that you can always add a property to a previously defined object. For example, the statement
car1.color = "black"
This applies when you create an
object and its properties with a constructor function, as in the above example
of the Car object type, and when you define individual properties
explicitly (for example, myCar.color = "red"). So if you define
object properties initially with an index, such as myCar[5] = "25
mpg", you can subsequently refer to the property as
myCar[5].
You can add a property to a previously defined object type by using the prototype property. This defines a property that is shared by all objects of the specified type, rather than by just one instance of the object. The following code adds a color property to all objects of type car, and then assigns a value to the color property of the object car1. For more information, see "prototype".
Car.prototype.color=null
car1.color="black"
birthday.description="The day you were born"
object.methodname = function_name
You can then call the method in the context of the object as follows:
object.methodname(params);
function displayCar() {
var result = "A Beautiful " + this.year + " " + this.make
+ " " + this.model
pretty_print(result)
}
where pretty_print is the function (defined in "Functions") to display a horizontal rule and a string. Notice the use of this to refer to the object to which the method belongs.
You can make this function a method of car by adding the statement
this.displayCar = displayCar;
to the object definition. So, the full definition of car would now look like
function car(make, model, year, owner) {
this.make = make
this.model = model
this.year = year
this.owner = owner
this.displayCar = displayCar
}
Then you can call the displayCar method for each of the objects as follows:
car1.displayCar()
car2.displayCar()
This will produce output like:
function validate(obj, lowval, hival) {
if ((obj.value < lowval) || (obj.value > hival))
alert("Invalid Value!")
}
<INPUT TYPE = "text" NAME = "age" SIZE = 3
onChange="validate(this, 18, 99)">
In general, this refers to the calling object in a method.
<FORM NAME="myForm">
Form name:<INPUT TYPE="text" NAME="text1" VALUE="Beluga">
<P>
<INPUT NAME="button1" TYPE="button" VALUE="Show Form Name"
onClick="this.form.text1.value=this.form.name">
</FORM>
| file: /Techref/language/java/SCRIPT/model.htm, 22KB, , updated: 2009/2/2 14:27, local time: 2025/10/24 15:39,
216.73.216.22,10-3-83-201:LOG IN
|
| ©2025 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://www.piclist.com/techref/language/java/SCRIPT/model.htm"> Object model </A> |
| Did you find what you needed? |
|
o List host: MIT, Site host massmind.org, Top posters @none found - Page Editors: James Newton, David Cary, and YOU! * Roman Black of Black Robotics donates from sales of Linistep stepper controller kits. * Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters. * Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated! * Contributors: Richard Seriani, Sr. |
|
Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232! |
.