JavaScript (Object Construction Method)

Source: Internet
Author: User

1. function object

JavaScript does not overload the concept that each object implies an arguments object (actually received parameter). The Length attribute receives the expected parameters of the object, arguments. length, indicating the number of actually received parameters;

VaR add = function (number) {alert (number );};

Equivalent to VaR add = new function ("number", "Alert (number)"); // The last parameter represents the function body.

Equivalent to function add (number) {alert (number )};

Because it is an object reference, multiple functions with the same name will be overwritten by the last one, regardless of the number of parameters.

2. Data Types: Undefined (undefined), null (null), number (number), string (string), Boolean (true/false) five original data types: var S = "A"; alert (typeof s) return 'string'

Typeof returned values include 'undefined', 'object, 'number', 'string', and 'boolean'

Javascript if the declared function has no return value or return value, the returned undefined-undefined is derived from null.

Forced type conversion: there are only three types of Boolean (value), string (value), number (value): The Return Nan numeric Conversion error. conversion is not allowed if it is not defined. boolean is useless, it is not recommended.

3. javascript can dynamically add or delete attributes.

Object OBJ = new object ();

OBJ. Username = "zhangsan"; // OBJ ["usernam"] = "zhangsan ";

Delete obj. Username ;//

VaR object = {Username: "zhangsan", password: "Lisi", Gender: function (){...}} // The most common way to define objects. attributes are separated by commas.

4. Array

VaR array = new array ();

Array. Push (1 );

Array. Length

// Var array = [1, 3, 23]; // more common

Arrays. Sort (array); //, 3, which is first converted into characters and sorted in ascending order of strings. This is obviously unreliable and generally sorted in custom order.

Similar to Java

Function compare (num1, num2)

{

Varnum1 = parseint (num1 );

Varnum2 = parseint (num2 );

If (num1 <num2) Return-1;

Elseif (num1> num2) return 1;

Else return 0;

}

VaR array = [1, 3, 25];

Array. Sort (compare); // function name is an object reference

Array à [1 3 25]

5. Object creation method (JavaScript only has the object concept)

A: // 1: expand based on existing objects

VaR object = {Username: "zhangsan", password: "Lisi", Gender: function (){...}} // The most common way to define objects. attributes are separated by commas.

--------------------------------------------------------------------------

VaR object = newobject (); // suitable for temporary objects

Object. Name = "zhangsan ";

Object. Password = "123 ";

Object. sayname = function (name)

{

This. Name = Name;

Alert (this. Name );

}

Object. sayname ("Lisi ");

B: // 2: factory Method

Function Createobject (name, password)

{

VaR object = newobject ();

Object. Name = Name;

Object. Password = password;

/*

Object. Get = function ()

{

Alert (this. Name + "," + this. Password );

}

*/

Object. Get = get; // The recommendation. attribute is unique and the method is shared.

Return object;

}

Function get ()

{

Alert (this. Name + "," + this. Password );

}

VaR obj1 = Createobject ("zhangsan", "123 ");

Obj1.get ();

VaR obj2 = Createobject ("Wanger", "3434 ");

Obj2.get ();

C: // 3: constructor Mode

Function person (name, password)

{

This. Name = Name;

This. Password = password;

This. getinfo = function ()

{

Alert (this. Name + "," + this. Password );

}

// Implicitly generated object

}

 

VaR P = newperson ("zhangsan", "Wanger ");

P. getinfo ();

D // 4: Use prototype. The attribute value must be assigned after the object is generated. The attribute reference method (value change) is the same as Java. The attribute is shared by other objects, this attribute can be changed for other objects. If it is a reference type, the constant type will not change.

Function student (){}

Student. Prototype. Name = "zhangsan ";

Student. Prototype. Password = "lkjldjfl ";

Student. Prototype. getinfo = function ()

{

Alert (this. Name + "," + this. Password );

}

VaR S1 = newstudent ();

S1.getinfo ();

E: // 5: Use the prototype + constructor to define the object (Implementation: attributes are not shared, and methods are shared)

Function person ()

{

This. Name = new array ();

This. Password = "123"; // remember to initialize when defining

}

Person. Prototype. getinfo = function ()

{

Alert (this. Name + "," + this. Password );

}

VaR p1 = new person ();

VaR P2 = new person ();

P1.name. Push ("James ");

P2.name. Push ("ris ");

P1.getinfo ();

P2.getinfo ();

6. Object Inheritance inheritance

// JavaScript inheritance: 1. Object impersonating

Function parent (name)

{

This. Name = Name;

This. sayname = function ()

{

Alert (this. Name );

}

}

Function Sun (name, age)

{

This. method = parent; // the method defined in prototype cannot be inherited and is not recommended.

This. Method (name );

Delete this. method;

This. Age = age;

This. sayage = function ()

{

Alert (this. Age );

}

}

 

VaR P = new parent ("zhangsan ");

VaR S = new sun ("Lisi", 20 );

P. sayname ();

S. sayname ();

S. sayage ();

Method 2: // The call method is a method in the function object. Therefore, every function we define has a call method. The first parameter of the call method is passed to this in the function, starting from the second parameter, the parameters will be passed to the function (caller) in sequence.

/** Test Call User

Function Test (str1, str2)

{

Alert (this. Name + "," + str1 + "," + str2)

}

VaR object = newobject ();

Object. Name = "zhangsan ";

Object. arg1 = 30;

Test. Call (object, "AA", 20 );

*/

// Use call to implement inheritance

Function daughter (name, gender)

{

Parent. Call (this, name );

This. Gender = gender;

This. getinfo = function ()

{

Alert (this. Gender );

}

}

// Test code

VaR d = new daughter ("Rose", 27 );

D. sayname ()

D. getinfo ();

Method 3 // use apply to implement inheritance, similar to call

Functiondaughter2 (name, gender)

{

Parent. Apply (this, new array (name,...); // [name], which transmits Parameters Using arrays, while the call method is discrete.

This. Gender = gender;

This. getinfo = function ()

{

Alert (this. Gender );

}

}

VaR D2 = newdaughter2 ("rose2", 37 );

D2.sayname ()

D2.getinfo ();

Method 4 // use prototype chain to implement hybrid inheritance recommendation

Function parent2 (name)

{

This. Name = Name;

}

 

Parent2.prototype. sayname = function ()

{

Alert (this. Name );

}

 

Function sun2 (name, age)

{

Parent2.call (this, name); // implement property inheritance

This. Age = age;

}

Sun2.prototype = new parent2 (); // inherits the prototype of the parent object

Sun2.prototype. sayage = function ()

{

Alert (this. Age );

}

// Test code

VaR sun2 = newsun2 ("Wang 'er", 100 );

Sun2.sayname ();

Sun2.sayage ();

7. firebug skills:

Function showlog (MSG)

{

Console. Log (MSG );

Lele.info (MSG );

Console. Warn (MSG );

Console. Error (MSG );

Console. debug (MSG );

}

Function Test (name)

{

Showlog (name );

}

Showlog ("test ");

8,

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.