a class defines a new type (formally, Abstract Data Type)
encapsulates data (properties) and operations on that data (methods)
a String encapsulates a sequence of characters, enclosed in quotes
properties include
length : stores the number of characters in the string
methods include
charAt(index) : returns the character stored at the given index
(as in C++/Java, indices start at 0)
substring(start, end) : returns the part of the string between the start
(inclusive) and end (exclusive) indices
toUpperCase() : returns copy of string with letters uppercase
toLowerCase() : returns copy of string with letters lowercase
to create a string, assign using new or just make a direct assignment (new is implicit)
word = new String("foo"); word = "foo";
properties/methods are called exactly as in C++/Java
word.length word.charAt(0)
这有帮助吗?
添加评论查看评论
问题 27. Tell us about local variables.
If needed, you can declare local variables within a function.
local variable is visible only within the function body after it’s declared.
Commonly used to store results of an intermediate calculation.
function findMaxValue(num1, num2,num3) {
var tempMax; //local var
if (num1 >= num2) {
tempMax = num1;
}
else {
tempMax = num2;
}
if(num3 >= tempMax) {
tempMax = num3;
}
return tempMax;
} //end function
这有帮助吗?
添加评论查看评论
问题 28. Explain Global Variables in Javascript.
Global variables are those declared outside of functions
Global variables are ‘visible’ from anywhere in the program, including inside functions
var globalHello = “Hello!”;
function writeHello() {
document.write(globalHello);
}
// outputs “Hello!”
这有帮助吗?
添加评论查看评论
问题 29. What are primitives in Javascript?
Boolean, string and number.
这有帮助吗?
添加评论查看评论
问题 30. How do you submit a form using Javascript?
Use document.forms[0].submit();
(0 refers to the index of the form – if you have more than one form in a page, then the first one has the index 0, second has index 1 and so on).