-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltin.html
292 lines (266 loc) · 9.94 KB
/
builtin.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<html>
<body>
<h1>Using Built-in Objects and Functions</h1>
<p>
The JavaScript Language contains the following built-in objects and functions:
<ul>
<li><a href="#string">String object</a>
<li><a href="#math">Math object</a>
<li><a href="#date">Date object</a>
<li><a href="#functs">Built-in functions</a>
</ul>
<p>
These objects and their properties and methods are built into the language.
You can use these objects in both client applications with Netscape Navigator
and server applications with LiveWire.
<a name="string"> <h2>Using the String Object</h2></a>
<p>
Whenever you assign a string value to a variable or property, you create a string object.
String literals are also string objects. For example, the statement
<pre>
mystring = "Hello, World!"
</pre>
<p>
creates a string object called mystring. The literal "blah" is also a string object.
<p>
The string object has methods that return:
<ul>
<li>a variation on the string itself, such as <i>substring</i> and <i>toUpperCase</i>.
<li>an HTML formatted version of the string, such as <i>bold</i> and <i>link</i>.
</ul>
<p>
For example, given the above object, <code>mystring.toUpperCase()</code> returns "HELLO, WORLD!",
and so does <code>"hello, world!".toUpperCase()</code>.
<a name="math"><h2>Using the Math Object</h2></a>
<p>
The built-in Math object has properties and methods for mathematical constants and
functions. For example, the Math object's PI property has the value of pi, which you
would use in an application as
<pre>
Math.PI
</pre>
<p>
Similarly, standard mathematical functions are methods of Math.
These include trigonometric, logarithmic, exponential, and other functions.
For example, if you want to use the trigonometric function sine, you would write
<pre>
Math.sin(1.56)
</pre>
<p>
Note that all trigonometric methods of math take arguments in radians.
<p>
It is often convenient to use the with statement when a section of code uses several math
constants and methods, so you don't have to type "Math" repeatedly. For example,
<pre>
with (Math) {
a = PI * r*r;
y = r*sin(theta)
x = r*cos(theta)
}
</pre>
<a name="date"><h2>Using the Date Object</h2></a>
<p>
JavaScript does not have a date data type. However, the date object and its
methods enable you to work with dates and times in your applications.
The date object has a large number of methods for setting, getting,
and manipulating dates. It does not have any properties.
<p>
JavaScript handles dates very similarly to Java.
The two languages have many of the same date methods, and both languages
store dates as the number of milliseconds since January 1, 1970 00:00:00.
<p>
<b>NOTE:</b>
You cannot currently work with dates prior to 1/1/70.
<p>To create a date object:
<pre>
<i>varName</i> = new Date(<i>parameters</i>)
</pre>
where <i>varName</i> is a JavaScript variable name for the date object being created;
it can be a new object or a property of an existing object.
<p>
The <i>parameters</i> for the Date constructor can be any of the following:
<ul>
<li>Nothing: creates today's date and time. For example,
<code>today = new Date()</code>
<li>A string representing a date in the following form:
"Month day, year hours:minutes:seconds". For example,
<code>
Xmas95= new Date("December 25, 1995 13:30:00")
</code>
If you omit hours, minutes, or seconds, the value will be set to zero.
<li>A set of integer values for year, month, and day.
For example,
<code>
Xmas95 = new Date(95,11,25)
</code>
<li>A set of values for year, month, day, hour, minute, and seconds
For example,
<code>
Xmas95 = new Date(95,11,25,9,30,0)
</code>
</ul>
<p>
The Date object has a large number of methods for handling dates and times.
The methods fall into these broad categories:
<ul>
<li>"set" methods, for setting date and time values in date objects
<li>"get" methods, for getting date and time values from date objects
<li>"to" methods, for returning string values from date objects.
<li>parse and UTC methods, for parsing date strings.
</ul>
<p>
The "get" and "set" methods enable you to get and set seconds, minutes, hours,
day of the month, day of the week, months, and years separately.
There is a getDay method that returns the day of the week, but no
corresponding setDay method, because the day of the week is set automatically.
These methods use integers to represent these values as follows:
<ul>
<li>seconds and minutes: 0 to 59
<li>hours: 0 to 23
<li>day: 0 to 6 (day of the week)
<li>date: 1 to 31 (day of the month)
<li>months: 0 (January) to 11 (December)
<li>year: years since 1900
</ul>
<p>
For example, suppose you define the following date:
<pre>
Xmas95 = new Date("December 25, 1995")
</pre>
Then <code>Xmas95.getMonth()</code> returns 11, and <code>Xmas95.getYear()</code>
returns 95.
<p>
The getTime and setTime methods are useful for comparing dates.
The getTime method returns the number of milliseconds since the epoch for
a date object.
<p>
For example, the following code displays the number of shopping days left until
Christmas:
<pre>
today = new Date()
nextXmas = new Date("December 25, 1990")
nextXmas.setYear(today.getYear())
msPerDay = 24 * 60 * 60 * 1000 ; // Number of milliseconds per day
daysLeft = (nextXmas.getTime() - today.getTime()) / msPerDay;
daysLeft = Math.round(daysLeft);
document.write("Number of Shopping Days until Christmas: " + daysLeft);
</pre>
<p>
This example creates a date object named today that contains today's date.
It then creates a date object named nextXmas, and sets the year to the
current year. Then, using the number of milliseconds per day, it computes
the number of days between today and nextXmas, using getTime, and rounding
to a whole number of days.
<p>
The parse method is useful for assigning values from date strings to existing
date objects. For example, the following code uses parse and setTime to
assign a date to the IPOdate object.
<pre>
IPOdate = new Date()
IPOdate.setTime(Date.parse("Aug 9, 1995"))
</pre>
<!---
In general, operating systems know what your current time zone is,
but time is stored internally as Greenwich Mean Time, GMT (also known as
Universal Coordinated Time or UTC).
myvar = new Date(args)--you give local date/time args.
getTime and setTime referenced to GMT.
myvar = new Date(Date.UTC(args))--you give GMT arguments.
--->
<a name="functs"><h2>Using Built-in functions</h2></a>
<p>
JavaScript has several "top-level" functions built-in to the language.
They are:
<ul>
<li>eval
<li>parseInt
<li>parseFloat
</ul>
<a name="eval"><h3>The eval Function</h3></a>
<p>
The built-in function <i>eval</i> takes a string as its argument.
The string can be is any string representing a JavaScript expression, statement,
or sequence of statements.
The expression can include variables and properties of existing objects.
<p>
If the argument represents an expression, eval evaluates the expression.
If the argument represents one or more JavaScript statements, eval performs the statements.
<p>
This function is useful for evaluating a string representing an arithmetic expression.
For example, input from a form element is always a string, but you often want to convert
it to a numerical value.
<p>
The following example takes input in a text field, applies the eval function and displays
the result in another text field.
If you type a numerical expression in the first field, and click on the button, the
expression will be evaluted. For example, enter "(666 * 777) / 3", and click on the button
to see the result.
<xmp>
<script>
function compute(obj) {
obj.result.value = eval(obj.expr.value)
}
</script>
<form name="evalform">
Enter an expression: <input type="text" name="expr" size="20">
<br>
Result: <input type="text" name="result" size="20">
<br>
<input type="button" value="Click Me" onclick="compute(this.form)">
</form>
</xmp>
<p>
<script>
function compute(obj) {
obj.result.value = eval(obj.expr.value)
}
</script>
<form name="evalform">
Enter an expression: <input type="text" name="expr" size="20">
<br>
Result: <input type="text" name="result" size="20">
<br>
<input type="button" value="Click Me" onclick="compute(this.form)">
</form>
<p>
The eval function is not limited to evaluating numerical expressions, however.
Its argument can include object references or even JavaScript statements.
For example, you could define a function called setValue that would take two arguments:
and object and a value, as follows:
<pre>
function setValue (myobj, myvalue) {
eval ("document.forms[0]." + myobj + ".value") = myvalue;
}
</pre>
<p>
Then, for example, you could call this function to set the value of a form element
"text1" as follows:
<pre>
setValue(text1, 42)
</pre>
<a name="parseInt"><h3>The parseInt and parseFloat Functions</h3></a>
<p>
These two built-in functions return a numeric value when given a string as an argument.
<p>
ParseFloat parses its argument, a string, and attempts to return a floating point number. If it encounters
a character other than a sign ( + or -), numeral (0-9), a decimal point, or an exponent, then it returns
the value up to that point and ignores that character and all succeeding characters. If the first character
cannot be converted to a number, it returns <i>NaN</i> (not a number).
<p>
The parseInt function parses its first argument, a string, and attempts to return an integer of the
specified radix (base). For example, a radix of 10 indicates to convert to a decimal number, 8 octal,
16 hexadecimal, and so on. For radixes above 10, the letters of the alphabet indicate numerals greater
than 9. For example, for hexadecimal numbers (base 16), A through F are used.
<p>
If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all
succeeding characters and returns the integer value parsed up to that point. If the first character cannot
be converted to a number in the specified radix, it returns <i>NaN.</i> ParseInt truncates numbers to integer
values.
</body>
</html>
<!--
FILE ARCHIVED ON 23:25:37 Jun 17, 1997 AND RETRIEVED FROM THE
INTERNET ARCHIVE ON 18:09:46 Nov 08, 2018.
ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
SECTION 108(a)(3)).
-->