JavaScript object-oriented support (1)

Related Tags:

  ================================================== ============================== 
  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 launched: aimingoo (aim@263.net) 
  Project Team: aimingoo, leon (pfzhou@gmail.com) 
  There are contributors: JingYu (zjy@cnpack.org) 
  ================================================== ============================== 


  8, the support of object-oriented JavaScript 
~~~~~~~~~~~~~~~~~~
  JavaScript few people on the characteristics of object-oriented systems analysis.    I hope that the next words to the language you understand that at least well known side. 


  1. JavaScript in the type of 
--------
  Although JavaScript is an object-based language, but the object (Object) in JavaScript is not the first type.  JS
  By function (Function) for the first type of language.    In saying this, not only because JS function in a high-level language function in the various characteristics, but also because in JS, Object is a function to achieve.    -- On this point, in the text after the "structural configuration and Analysis" to see further clarification. 

  JS is weak type, and he's built-in types of simple and clear: 
  -------------------------------------------------- ---------- 
  Undefined: undefined 
  Number: Digital 
  Boolean: boolean 
  String: string 
  Function: Function 
  Object: Object 

  1). Undefined type 
  =================== 
  IE5 and in the following version, in addition to the direct assignment and typeof (), any other undefined on the operation would lead to abnormal.    If one needs to know whether a variable is undefined, but to use typeof () method: 
<script>
  Var v; 
  If (typeof (v) == 'undefined') ( 
  / / ... 
  ) 
  </ Script> 

  However, in IE5.5 and above, undefined is a reservation system has been realized characters.    Undefined therefore can be used to compare and computing.    Test whether a value is undefined more simple method can be: 
<script>
  Var v; 
  If (v === undefined) ( 
  / / ... 
  ) 
  </ Script> 

  Therefore, to make the core code can be (partially) and compatible with earlier versions of IE5, Romo core modules in a code for "declaration" an undefined value: 
  //------------------------------------------------ ----------- 
  / / Code from Qomolangma, in JSEnhance.js 
  //------------------------------------------------ ----------- 
  Var undefined = void null; 

  Another point of this line of code is necessary to point out that void statements applications.    Void that "the implementation of the subsequent statements, and ignore the return value."    Therefore there can be void after the implementation of any "individual" statement.    And the results of implementation is 
  Undefined.    Of course, if you wish, you can use the following code of the definition of "undefined." 
  //------------------------------------------------ ----------- 
  / / 1. More complex, using an anonymous function of the implementation of the return air 
  //------------------------------------------------ ----------- 
  Var undefined = function (){}(); 

  //------------------------------------------------ ----------- 
  / / 2. Code more simple, but not easy method 
  //------------------------------------------------ ----------- 
  Var undefined void = 0; 

  Void can also function as the same use, and thus void (0) is legitimate.    In some cases, the number of complex sentences may not be able to use the keyword void forms, and must use the form of a function of void.    For example: 
  //------------------------------------------------ ----------- 
  / / Must use void () complex forms of expression 
  //------------------------------------------------ ----------- 
  Void (i = 1); / / or the following statement: 
  Void (i = 1, i); 


  2). Type number 
  =================== 
  JavaScript always handle the float, and therefore it does not like the Delphi MaxInt such constants, it is this definition of the two values: 
  Number.MAX_VALUE: Back JScript can express the greatest number.    Equivalent to about 1.79 E 308. 
  Number.MIN_VALUE: Back JScript closest to the number 0.    Equivalent to about 2.22 E-308. 

  Because there is no cosmetic reasons, in some css and DOM on the attributes of computing, if you expect to integer values 2, 
  You may be the string "2.0" - or in some cases similar to this.    Under such circumstances, you may need to use the global object (Gobal) parseInt () method. 

  Global Object (Gobal) there are two types of attributes and the number of the computing: 
  NaN: arithmetic expressions is not the result of the operation, while the number of return NaN value. 
  Infinity: MAX_VALUE more than a few. 

  If a value is NaN, then he can be the global object (Gobal) isNaN () method to detect.    However, the two NaN 
  Value is not between the reciprocal.    The following cases: 
  //------------------------------------------------ ----------- 
  / / NaN computing and Detection 
  //------------------------------------------------ ----------- 
  Var 
  V1 = 10 * 'a'; 
  V2 = 10 * 'a'; 
  Document.writeln (isNaN (v1)); 
  Document.writeln (isNaN (v2)); 
  Document.writeln (v1 = v2); 

  Global Object (Gobal) than Infinity said the largest number (Number.MAX_VALUE) greater value.    In JS, 
  In math, and the value of Infinity is the same.    -- In some practical skills, it can also be used to do an array of sequence boundary detection. 

  Infinity in the Number object is defined as POSITIVE_INFINITY.    In addition, the negative Infinite Number is defined in: 
  Number.POSITIVE_INFINITY: is the largest ratio (Number.MAX_VALUE) greater value.    Infinity. 
  Number.NEGATIVE_INFINITY: the smallest negative (- Number.MAX_VALUE) smaller value.    Negative infinity. 

  And NaN difference is that the two Infinity (or - Infinity) between mutual reciprocity.    The following cases: 
  //------------------------------------------------ ----------- 
  / / Infinity computing and Detection 
  //------------------------------------------------ ----------- 
  Var 
  V1 = Number.MAX_VALUE * 2; 
  Number.MAX_VALUE = v2 * 3; 
  Document.writeln (v1); 
  Document.writeln (v2); 
  Document.writeln (v1 = v2); 

  Global and the number of other types of related methods are: 
  IsFinite (): If the value is NaN / Infinity / negative infinity back to the false, or else return to true. 
  ParseFloat (): string (prefix) from a float.    Returns NaN unsuccessful. 


  3). Boolean type 
  =================== 
  (Omitted) 

  4). String type 
  =================== 
  JavaScript's String type did not any special, but the JavaScript in order to adapt to "the realization of the browser hypertext environment", it has some strange ways.    For example: 
  Link (): HREF attribute to a hyperlink on the label <A> String object in the text ends. 
  Big (): the one pair of <big> labels on the String object in the text ends. 
  Following the same method and such: 
  Anchor () 
  Blink () 
  Bold () 
  Fixed () 
  Fontcolor () 
  Fontsize () 
  Italics () 
  Small () 
  Strike () 
  Sub () 
  Sup () 

  In addition, the main complexity of the string from the omnipresent in JavaScript toString () 
  Methods.    This is JavaScript browser environment for the provision of a very important method.    For example, we declare an object, but in document.writeln () to output it will be displayed in IE? 

  Under this example: 
  //------------------------------------------------ ----------- 
  / / ToString () Application 
  //------------------------------------------------ ----------- 
  Var 
  S = new Object (); 

  S.v1 = 'hi,'; 
  S.v2 = 'test!'; 
  Document.writeln (s); 
  Document.writeln (s.toString ()); 

  S.toString = function () ( 
  Return s.v1 s.v2; 
  ) 
  Document.writeln (s); 

  In this example, we see that when an object does not re-statement (cover) their toString () method, then use it as a string pattern, (such as writeln), will be called Java Script 
  Environmental default toString ().    Conversely, you can redefine JavaScript understanding of this object methods. 

  Many JavaScript framework in the implementation of the "template" mechanism, the use of this characteristic.    For example, they use such a definition FontElement Target: 
  //------------------------------------------------ ----------- 
  / / Use toString () realize the simple principle of template mechanism 
  //------------------------------------------------ ----------- 
  Function FontElement (innerHTML) ( 
  This.face = '10 lines'; 
  This.color = 'red'; 
  / / More ... 

  InnerHTML = var ctx; 
  This.toString = function () ( 
  Return '<Font FACE="' this.face'" COLOR="' this.color'">' 
  Ctx 
  '</ FONT>'; 
  ) 
  ) 

  Var obj = new FontElement ( 'This is a test.'); 

  / / Note that the following lines of code written 
  Document.writeln (obj); 


  5). Type function 
  =================== 
  Javascript function has many characteristics, in addition to the object-oriented part of (this in the back on), which have their own unique characteristics of applications are very broad. 

  First javascript for each function, the process of calling a bailiff arguments can be targeted.    This object is explained by the environment created by the script, you have no other way to create their own arguments a target. 

  Arguments can be viewed as an array: its length attributes, and can be arguments [n] each way to visit a parameter.    However, its most important, however, is to get through the callee property being implemented by the function object. 

  Took a very interesting problem: there is a caller Function object attributes, at the current function is called the father function object. 

  -- We have already seen that we can JavaScript inside through the callee / Ergodic caller to the call stack implementation period.    Function arguments are due to the fact an attribute, so we in fact can also traverse execution of the call stack each function parameters.    The following code is a simple example: 

  //------------------------------------------------ ----------- 
  / / Call stack Ergodic 
  //------------------------------------------------ ----------- 
  Function foo1 (v1, v2) ( 
  Foo2 (v1 * 100); 
  ) 

  Function foo2 (v1) ( 
  Foo3 (v1 * 200); 
  ) 

  Function foo3 (v1) ( 
  Var foo = arguments.callee; 
  While (foo & & (foo! = Window)) ( 
  Document.writeln ( 'call parameters: <br>''---------------< br> '); 

  Var args = foo.arguments, argn = args.length; 
  For (var i = 0; i <argn; i) ( 
  Document.writeln ( 'args [' i, ']:', args [i], '<br>'); 
  ) 
  Document.writeln ( '<br>'); 

  / / On a 
  Foo = foo.caller; 
  ) 
  ) 

  / / Testing 
  Foo1 (1, 2); 


  2. JavaScript object-oriented support 
--------
  In the previous example has been referred to the type of object "type statement" and the "creation of an example." 
  In JavaScript, we need a function to declare their own object types: 
  //------------------------------------------------ ----------- 
  / / JavaScript in the type of object code in the form of a statement 
  / / (After the document, the "Object" is usually used to replace MyObject) 
  //------------------------------------------------ ----------- 
  Function object (parameter table) ( 
  This. Attribute = initial value; 

  This. Method = function (method parameter table) ( 
  / / Method code 
  ) 
  ) 


  Then, we can create such a code of this type of an object instance: 
  //------------------------------------------------ ----------- 
  / / Create examples in the form of code 
  / / (After the document, the "instance variables were" normally used to replace obj) 
  //------------------------------------------------ ----------- 
  Var examples of the variable name = new Object (parameter table); 


  Next we look at the "target" in a number of specific JavaScript achieve and strange characteristics. 

  1). Function in JavaScript object-oriented mechanisms in the five-identity 
------
  "Object" - such as MyObject () - This function role as the following language: 
  (1) General function 
  (2) type statement 
  (3) the realization of type 
  (4) categories cited 
  (5) Object constructor function 

  Some programmers (such as Delphi programmers) are used to separate type of statement and Implementation.    For example, in the delphi 
  , Interface type of statement for or variable, and implementation section for writing types of codes, or some function for the implementation of the code flow. 

  But in JavaScript, and the type of statement is the realization of mixing.    The type of an object () 
  Statement by function, this.xxxx show that the target may have the attributes or methods. 


  This function is also "cited."    In JavaScript, if you need to identify a specific object type, you need to have a bailiff "quote."    -- Of course, also is the name of this function.    Instanceof operator to be used to identify the types of examples, we look at the application: 
  //------------------------------------------------ ----------- 
  / / JavaScript in the type of object recognition 
  / / Syntax: Object class cited examples instanceof 
  //------------------------------------------------ ----------- 
  Function MyObject () ( 
  This.data = 'test data'; 
  ) 

  / / Here MyObject () constructor is used as a 
  Var obj = new MyObject (); 
  Var arr = new Array (); 

  / / Class quoted here as the use of MyObject 
  Document.writeln (obj instanceof MyObject); 
  Document.writeln (arr instanceof MyObject); 

================
  (To be continued) 
================
  Next: 

  2. JavaScript object-oriented support 
--------

  2). Reflex mechanism in the realization JavaScript 
  3). This and with the use of keywords 
  4). Keyword use in computing 
  5). Use instanceof keyword computing 
  6). Object-oriented and other related keywords 

  3. Tectonic structure and Analysis 

  4. Examples cited examples and 

  5. Prototype issue 

  6. Function in the context of the environment 

  7. Object type checking problems 

Related articles:

Beginners Guide to HTML - Designation title
  Original content of this site, please specify the source of reproduced I love DivCSS.    All the HTML pages should have Designation.    Add your name to the website, you change the code so that it looks like Figure:    Here, we add two elements: <head> ...
Help attract repeat customers your website 10:00 skills
  Year who is the most "Chixinyajiu", the answer is "netizens"!    Is it not?    Web site operators are racking their brains to a beautifully designed and affinity pages, and planning fun while Bocaiyinchang channels, development of the amazing and usefu...
Web2.0 on B2B website operators bring ideas on what changes
  Note: The article inside on all WEB2.0 understanding of website operators are thinking that the conversion.    Operating the so-called 1.0, is the operator-driven Web site move forward, users in the passive role; the so-called 2.0, is the operator invite users to promote web site a...
Very detailed IMG, IFRAME attributes reference manual
  I element | i object IMG element | img DHTML Object Object    IFRAME element | iframe object    -------------------------------------------------- ------------------------------    Create embedded floating framework.    Table members    ...
Increased affinity and easy access - to control the font size of the decision-making power back to the user
  Now the majority of the sites are domestic use CSS set the font size to 12 px, although the relative size of pixel units, but only in relation to the resolution of the screen when a certain screen resolution, the word if you are too small to read difficulties in the non-standard browser on t...