1、JScript是一種具有松耦合性基於對象的指令碼語言。所謂松耦合語言,既是不需要顯示的申明對象的類型,甚至,很多情況下,在需要的時候,對象的類型可以自動轉換為我們需要的。
2、建議總是在使用對象前申明對象,如果在var申明中沒有初始化對象,自動取值為:undefined。var關鍵字並不是必須的,但是,如果你忽略了var,則變數就是全域變數。當你要申明一個局部變數(函數,過程等內部)時,就必須使用var來限制該變數的作用地區。
3、在計算中,JScript會強制的轉換資料類型,例:
var x = 2000; // A number.var y = "Hello"; // A string.x = x + y; // the number is coerced into a string.document.write(x); // Outputs 2000Hello.
要顯示的將字元轉換為int,請使用:parseInt Method. To explicitly convert a string to a number, use the parseFloat Method.
4、在JScript中,有3中未經處理資料類型(String、Number、Boolean),兩個組合資料類型(Object Array)以及兩個特殊資料類型( Null、Undefined )。
5、Undefined Data Type
The undefined value is returned when you use:
- an object property that does not exist,
- a variable that has been declared, but has never had a value assigned to it.
6、Null Data Type
The null data type has only one value in JScript: null. The null keyword cannot be used as the name of a function or variable.
A variable that contains null contains "no value" or "no object." In other words, it holds no valid number, string, Boolean, array, or object. You can erase the contents of a variable (without deleting the variable) by assigning it the null value.
7、函數
JScript包括兩類函數,其一是JScript內建函數,另一個就是使用者自動一個函數。
8、自訂對象的建立
// pasta is a constructor that takes four parameters.// The first part is the same as abovefunction pasta(grain, width, shape, hasEgg){ // What grain is it made of? this.grain = grain; // How wide is it? (number) this.width = width; // What is the cross-section? (string) this.shape = shape; // Does it have egg yolk as a binder? (boolean) this.hasEgg = hasEgg; // Here we add the toString method (which is defined below). // Note that we don't put the parentheses after the name of // the function; this is not a function call, but a // reference to the function itself. this.toString = pastaToString;}// The actual function to display the contents of a pasta object. function pastaToString(){ // return the properties of the object return "Grain: " + this.grain + "\n" + "Width: " + this.width + "\n" + "Shape: " + this.shape + "\n" + "Egg?: " + Boolean(this.hasEgg);}
// Additional properties for spaghetti instance.spaghetti.color = "pale straw";spaghetti.drycook = 7;spaghetti.freshcook = 0.5;
// new :建立自訂類型對象執行個體 var chowFun = new pasta("rice", 3, "fl at", false); // Neither the chowFun object, nor any of the other existing// pasta objects have the three new properties that were added// to the spaghetti object.// Adding the 'foodgroup' property to the pasta prototyp object// makes it available to all instances of pasta objects, // including those that have already been created.pasta.prototype.foodgroup = "carbohydrates"// now spaghetti.foodgroup, chowFun.foodgroup, etc. all// contain the value "carbohydrates"
9、Microsoft JScript provides eleven intrinsic (or "built-in") objects. They are the Array, Boolean, Date, Function, Global, Math, Number, Object, RegExp, Error, and String objects. Each of the intrinsic objects has associated methods and properties that are described in detail in the language reference. Certain objects are also described in this section.
10、Objects as Arrays
在JScript中對象和數組幾乎是一致的,兩者都可以具有任意的屬性,實際上數組可以認作對象的一個特例。Arrays and Objects的區別在於Arrays具備一個length資料,當想資料的length+1位置賦值後,數組會自動更變。而如果設定length-n,則會截斷現有的資料。
//All objects in JScript support "expando" properties, or properties that can be added and removed dynamically at run time. //These properties can have any name, including numbers. If the name of the property is a simple identifier<<ref for identifier rules>>, //it can be written after the object name with a period, such as:var myObj = new Object();// Add two expando properties, 'name' and 'age'myObj.name = "Fred";myObj.age = 42;//If the name of the property is not a simple identifier, or it is not known at the time you write the script, //you can use an arbitrary expression inside square brackets to index the property. //The names of all expando properties in JScript are converted to strings before being added to the object.var myObj = new Object();// Add two expando properties that cannot be written in the// object.property syntax.// The first contains invalid characters (spaces), so must be// written inside square brackets.myObj["not a valid identifier"] = "This is the property value";
11、When you write a constructor, you can use properties of the prototype object (which is itself a property of every constructor) to create inherited properties, and shared methods. Prototype properties and methods are copied by reference into each object of a class, so they all have the same values. You can change the value of a prototype property in one object, and the new value overrides the default, but only in that one instance. Other objects that are members of the class are not affected by the change. Here is an example that makes use of the custom constructor, Circle (note the use of the this keyword).
13、Special Characters
Escape Sequence Character
\b Backspace
\f Form feed
\n Line feed (newline)
\r Carriage return
\t Horizontal tab (Ctrl-I)
\' Single quotation mark
\" Double quotation mark
\\ Backslash
小結:JScript是一指令碼語言,同其它語言一樣,都具有for、while、swich、if等流程。區別在於表示的方式不同以及它的松耦合性,例c# =>> foreach in 和 JScript=>> for in。