The third day of YUI. Ext Learning

Source: Internet
Author: User

For a lot of ambiguous knowledge in the past, it is necessary to repeat it: start with the most basic variable.

1 .. Variable

A. Global variables Global Variable and local variables Private Variable

There is a difference between adding var and without var.
-- If the keyword var is not used and a variable is declared, this is a global variable and can be accessed by any sub-function, even if it is out of curly brackets;
-- If the keyword var is used, all functions can access the variable starting from the braces {}. For example:


Function foo (){
I = 8 // Global Variable
Alert (I)
}

Function foo2 (){
Var I = 88 // variable I can be accessed under this curly brackets
Alert (I );
Child ()
Function child () {alert (I )}
}
Foo (); foo2 ()
Alert (I) // here I is still 8

It is worth noting that a function is an object during the compilation period and must be executed or instantiated before this variable can be allocated in the memory.
Global variables are used to start with "_". It is best to use them with caution for all p.s variables. You know exactly when the variable will change!
------- See the js manual:
"Although not secure, ignoring the var keyword in a declaration statement is a legal JScript syntax. In this case, the JScript interpreter gives visibility to the global scope of the variable. When a variable is declared at the process level, it cannot be used for the global range; in this case, the variable declaration must use the var keyword ."


B. Data Type of the variable the types of variable

Jscript has three main data types, two composite data types, and two special data types.

The main (basic) data types are:

String
Value
Boolean
The composite (reference) data type is:

Object
Array
The special data types are:

Null
Undefined
The following describes various object types in JavaScript:
Native Object: objects provided by the JavaScript language that do not depend on the execution host. Some of them are built-in objects, such as Global and Math. Some are created and used in the script running environment, such: array, Boolean, Date, Function, Number, Object, RegExp, and Error.
Build-in Object: built-in objects provided by the JavaScript language that do not depend on the execution host, such as Global and Math. All built-in objects are Native objects.
Host Object: Any objects provided by the JavaScript language that depend on the Host environment. All non-Native objects are Host objects, such as Windows in IE and WScript instances in wscript, any class created by the user.



* *** How to check the object type? ******
1. typeof ()
The typeof operator returns the type information as a string. There are six possible types of typeof return values: "number," "string," "boolean," "object," "function," and "undefined ."

2. val instanceof Array
Returns a Boolean value indicating whether the object is an instance of a specific class.
For example, to check the array or date type (in fact, any type is allowed, see the example), you must use instance of + class name (no quotation marks), for example:

Function foo (){}
Var f = new foo ();
Alert (f instanceof foo2) // false

3. constructor
The usage of constructor is the same as that of instance, but the boolean value is not returned.

X = new String ("Hi ");
If (x. constructor = String)
// Process (the condition is true ).

* ****** What type is var I =? ****
Answer: The object type is equivalent to var I = new Object.

Object objects are the carriers of all objects. Think about the parent class.

The Object is very simple,
It only has two properties and two methods.
The two properties are:
Prototype
Constructor
The two functions are:
ToString ()
ValueOf ()

So how to implement var obj = new MyObject? In fact, it is also very simple. The regionalization definition of obj is as follows:



Var obj =
{
Properties1: 1, Properties2: '2', Properties3: [3],
Method1: function () {return this. Properties1 + this. Properties3 [0];},
Method2: function () {return this. Preperties2 ;}
};



The syntax defined by class instance regionalization is to use a pair of "{}" to represent the class, that is, "{}" is equivalent to "new Object ()". Then "{}" organizes attributes and methods by "key: value", the key can be any [A-Za-z0-9 _] character combination, or even the beginning of the number is legal @_@, value is any legal regionalized JavaScript data, and each key-value pair is separated.
It is usually used for JSON data exchange.


* ****** Two meanings of undefined *****
1. undefined keyword 2. undefined attributes
Variables are declared, but no value is assigned. This is the first case;
If there is no declaration, a variable is run for calculation. Its data type is the second case;
The two names have different meanings. We recommend that you change the name of the next version.


Var declared; // declare the variable.
If (declared = undefined) // changing to uninitialized makes initialization more accurate. js is loose language indeed!
Document. write ("declared has not been given a value .");

If (typeOf (notDeclared) = "undefined ")
Document. write ("notDeclared has not been defined .");

Related Article

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.