Javascript object-oriented Support (7)

Source: Internet
Author: User

========================================================== ==========================================================
Qomolangma openproject v0.9

Category: rich Web Client
Key words: JS Oop, JS framwork, rich Web Client, Ria, Web component,
Dom, dthml, CSS, JavaScript, JScript

Project Initiation: aimingoo (aim@263.net)
Project Team: aimingoo, Leon (pfzhou@gmail.com)
Contributor: Jingyu (zjy@cnpack.org)
========================================================== ==========================================================

VIII. Javascript object-oriented support
~~~~~~~~~~~~~~~~~~
(Continued)

7. Additional content supported by JavaScript object-oriented
--------
1). Type System
======================================
We have already fully described the two types of JavaScript systems. Including:
-Basic Type System: six basic types returned by typeof ()
-Object Type System: A type system organized by the new () return value, constructor, and prototype

Javascript is a weak type language, so automatic type conversion is an important part of its language features. However
For a specified variable (at a certain time point), it always has a definite data type. "Operation" is
The method that leads to type conversion (but not the root cause). Therefore, it is very important to determine the type of the calculation result. Off
In this part, we recommend that you read the following materials:
Http://jibbering.com/faq/faq_notes/type_convert.html

There is also a special component in the type system, that is, the "Direct Volume" declaration. The following code briefly describes
The method of volume claim, but no more details:
//---------------------------------------------------------
// Various direct volume declarations (see the JScript manual for some error formats or special cases)
//---------------------------------------------------------
// 1. Number
VaR n1 = 11; // normal decimal number
VaR n2 = 013; // Number of octal nodes
VaR N3 = 0xb; // hexadecimal number
VaR N4 = 1.2; // floating point value
VaR N5 =. 2; // floating point value
VaR N6 = 1.0e-4; // (or 1e-4) floating point value

// 2. String
VaR S1 = 'test'; // (or "test") String
VaR S2 = "test/N"; // a string with an escape character (for escape rules, see the manual)
VaR S3 = "'test'"; // use "", ''to use quotation marks in strings
VaR S4 = "/XD"; // Use escape characters to declare untyped characters

// 3. Boolean
VaR b1 = true;
VaR b2 = false;

// 4. Function
Function foo1 () {}; // directly declare using compiler features
VaR foo2 = function () {}; // declare an anonymous Function

// 5. Object
// * Pay attention to the use of the separator "," in the statement.
VaR obj1 = NULL; // empty objects can be directly declared.
VaR obj2 = {
Value1: 'value', // Object Property
Foo1: function () {}, // uses anonymous functions to directly declare object Methods
Foo2: foo2 // point the method to a declared function
}

// 6. Regexp
VaR R1 =/^ [o | o] n/; // use a pair of "/../" to express a regular expression.
VaR r2 =/^./Gim; // (Note) Gim is the three parameters of the regular expression.

// 7. Array
VaR arr1 = [1, 1]; // directly declare, including some "undefined" Values
VaR arr2 = [1, [1, 'a']; // array declaration of heterogeneous (non-single type)
VaR arr3 = [[1], [2]; // multi-dimensional array (actually derived from the previous concept)

// 8. undefined
VaR U1 = undefined; // can be directly declared. Here, undefined is the global attribute.

In some cases, we can use the statement directly. The following Code demonstrates this feature:
//---------------------------------------------------------
// Directly add "I. e. Declaration is used"
//---------------------------------------------------------
VaR OBJ = function () {// 1. declared an anonymous Function
Return {// 2. The result of function execution is to return an object directly declared"
Value: 'test ',
Method: function (){}
}
} (); // 3. Run the anonymous function and return the result to declare the OBJ variable.

In this example, a direct volume statement is used in many cases. In this example, the function is declared directly (and can be executed immediately)
Is very valuable, for example, trying to execute some code in A. js file, but do not want the variable declaration in the code
Global Code causes an impact. Therefore, you can wrap an anonymous function in the outer layer and execute it. For example:
//---------------------------------------------------------
// Anonymous function execution
// (Note: void is used to enable subsequent functions to be executed. Otherwise, the interpreter considers it to be a declaration function only)
//---------------------------------------------------------
Void function (){
If (isie ()){
// Do something...
}
}();

2). Object System
======================================
An unmentioned important part of the Object System is the delete operation. It is used to delete array elements, object attributes, and
Declared variable.

Because the delete operation cannot delete the variables declared using VAR, it means that it can only delete the internal and external declarations in the function.
. -- This statement is a bit awkward, but it does. Then, we can further look
Really think: the essence of deleting a variable in the delete operation is to delete the owner declared in the context of the window object.
.

Back to the previous discussion about "context", we noticed three forms of global variable Declaration (outside the function:
----------
VaR global_1 = 'Global variable 1 ';
Global_2 = 'Global variable 2 ';

Function Foo (){
Global_3 = 'Global variable 3 ';
}
----------

Global variables 2 and 3 are "variables declared without Var", which is actually in the context of the window object
Attribute declaration. That is to say, you can use window. global_2 and window. global_3 to access them. These three voices
The method of the properties of the Ming window object, and the method of directly specifying "window. global_value = <value>"
The only difference is that during the "for... in" operation, the attributes/methods declared by these three methods are hidden. As follows:
Example:
//---------------------------------------------------------
// Some features of the global variable Context Environment: property name hiding
//---------------------------------------------------------
VaR global_1 = 'Global variable 1 ';
Global_2 = 'Global variable 2 ';

Void function Foo (){
Global_3 = 'Global variable 3 ';
}();

Window. global_4 = 'Global variable 4 ';

For (var I in window ){
Document. writeln (I, '<br> ');
}
Document. writeln ('<HR> ');
Document. writeln (window. global_1, '<br> ');
Document. writeln (window. global_2, '<br> ');
Document. writeln (window. global_3, '<br> ');

We noticed that the attribute name of global variable 1/2/3 is not displayed in the returned results. However, window. XXXX is used.
They can still be accessed.

In the window context, global_1 is essentially a private variable in the context.
Get it, just because other (all) code is within this context. Global_2/3 is (implicitly) declared
Window properties, while global_4 explicitly declares window properties.

Therefore, we will return to the previous conclusion:
-The essence of deleting a variable (without var Declaration) is to delete the properties of the window object.

In addition, we get three additional inferences (the most important one is the first one ):
-Delete can delete array elements because the array subscript is also an implicit attribute of the array object.
-In a complex system, to reduce variable name conflicts, avoid the use of global variables (and declarations) or
Delete operation to clear the properties of the window object.
-The window object is the only object that allows users to declare "implicit attributes. -- Note that this is only the appearance
Because, in fact, this is only an "additional effect" brought about by the Javascript specification ". :)

Delete clears "user declaration attributes" of window objects, system objects, and user objects, but does not clear objects such as prototype,
System attributes such as constructor. In addition, delete can also clear the elements in the array (but it does not clear the elements because
). For example:
//---------------------------------------------------------
// Examples of delete operations
//---------------------------------------------------------
VaR arr = [1, 2, 3];
VaR OBJ = {v1: 1, V2: 2 };
Global_variant = 3;

Delete arr [2];
Document. writeln ('1' in arr, '<br>'); // array subscript is actually an implicit attribute of an array object.
Document. writeln (ARR. length, '<br>'); // The array length will not change due to delete.

Delete obj. V2;
Document. writeln ('v2' in OBJ, '<br> ');

Document. writeln ('Global _ variant 'in window,' <br> ');
Delete global_variant;

// The following Code cannot be executed normally, which is a bug in IE.
If ('Global _ variant' in window ){
Document. writeln ('bug test: ', global_variant,' <br> ');
}

The root cause of the code error in the last line is that IE mistakenly detects the object attribute of 'global _ variant 'in the window.
. Because at the same position, the return value of the "('Global _ variant' in window)" expression
The returned result is true! -- Firefox does not have this bug.

Deleting an attribute or array element in Delete does not indicate that the script engine will analyze the attribute or element. Object
The Destructor is uncertain. For more information about this, see the previous section.

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.