JScript-ecmascript Edition 3 Study Notes (BASICS)

Source: Internet
Author: User
Tags types of functions

1. JScript is a loosely coupled object-based scripting language. The so-called loosely coupled language is the type of declarative object that does not need to be displayed. In many cases, the object type can be automatically converted to what we need when necessary.

2. We recommend that you always declare the object before using the object. If the object is not initialized in the VaR statement, the automatic value is:Undefined.The VaR keyword is not mandatory. However, if you ignore var, the variable is a global variable. When you want to declare a local variable (function, process, and so on), you must use VaR to limit the region of the variable.

3. In computing, JScript forcibly converts the data type, for example:

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.
To convert the character to int, use: parseint method. To explicitly convert a string to a number, use the parsefloat method.
4. In JScript, there are three primitive data types (string, number, and Boolean), two combined data types (Object array), and two special data types (null and 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

TheNullData 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. Functions

JScript includes two types of functions: one is the built-in JScript function, and the other is the user's automatic function.

8. Create a custom object

// 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: create a user-defined object instance 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 'food' 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 areArray,Boolean,Date,Function,Global,Math,Number,Object,Regexp,Error, AndStringObjects. 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

In JScript, objects and arrays are almost the same. Both of them can have any attribute. In fact, Arrays can be considered as special cases of objects. The difference between arrays and objects is that arrays has a length data. When you want to assign values to the length + 1 position of the data, the array will automatically change. If length-N is set, the existing data is truncated.

//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

Summary: JScript is a scripting language. Like other languages, JScript has processes such as for, while, Swich, And if. The difference is that the representation is different and Its loose coupling. For example, C #=>> foreach in and JScript =>> for in.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.