JScript-ECMAScript Edition 3 学习笔记(基础)

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 above
function 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。

posted @ 2008-12-02 08:34  俩醒叁醉  阅读(255)  评论(0编辑  收藏  举报