Java programmers create and inherit JavaScript objects from dummies to cainiao (29)

Source: Internet
Author: User

Create a JavaScript Object

 

Several Methods for defining objects in JavaScript (JavaScript does not have the concept of classes, but only objects ):

1) Expand its attributes and methods based on existing objects:

var object = new Object();object.name = "zhangsan";object.sayName = function(name){this.name = name;alert(this.name);}object.sayName("lisi");

 

2) Factory method

// Create an object in the factory mode/* function Createobject () {var object = new object (); object. username = "zhangsan"; object. password = "123"; object. get = function () {alert (this. username + "," + this. password);} return object;} var object1 = Createobject (); var object2 = Createobject (); object1.get ();

Constructor with parameters:

function createObject(username, password){var object = new Object();object.username = username;object.password = password;object.get = function(){alert(this.username + ", " + this.password);}return object;}var object1 = createObject("zhangsan", "123");object1.get();

 

Share a function object with multiple objects, rather than having one function object for each object.

function get(){alert(this.username + ", " + this.password);}function createObject(username, password){var object = new Object();object.username = username;object.password = password;object.get = get;return object;}var object = createObject("zhangsan", "123");var object2 = createObject("lisi", "456");object.get();object2.get();

 

3) constructor Method

Function person () {// before executing the first line of code, the JS engine will generate an object for us this. username = "zhangsan"; this. password = "123"; this. getinfo = function () {alert (this. username + "," + this. password);} // there is a hidden return statement to return the previously generated object} var person = new person (); person. getinfo ();

 

You can pass parameters when constructing objects.

function Person(username, password){this.username = username;this.password = password;this.getInfo = function(){alert(this.username + ", " + this.password);}}var person = new Person("zhangsan", "123");person.getInfo();

 

4) Prototype

// Use prototype to create an object/* function person () {} person. prototype. username = "zhangsan"; person. prototype. password = "123"; person. prototype. getinfo = function () {alert (this. username + "," + this. password);} var person = new person (); var person2 = new person (); person. username = "Lisi"; person. getinfo (); person2.getinfo ();*/

function Person(){}Person.prototype.username = new Array();Person.prototype.password = "123";Person.prototype.getInfo = function(){alert(this.username + ", " + this.password);}var person = new Person();var person2 = new Person();person.username.push("zhangsan");person.username.push("lisi");person.password = "456";person.getInfo();person2.getInfo();

If you use a prototype object, all the generated objects share the attributes in the prototype, so that an object changes this attribute and is also reflected in other objects.

An object defined by prototype alone cannot assign an initial value to the attribute in the constructor. The attribute value can only be changed after the object is generated.

Objects are defined using prototype + constructor. Attributes of objects do not interfere with each other. Objects share the same method.

// Use the prototype + constructor to define the object function person () {This. username = new array (); this. password = "123";} person. prototype. getinfo = function () {alert (this. username + "," + this. password);} var P = new person (); var P2 = new person (); p. username. push ("zhangsan"); p2.username. push ("Lisi"); p. getinfo (); p2.getinfo ();

 

5) Dynamic Prototype: In the constructor, all objects share a method with the flag, and each object has its own attributes.

function Person(){this.username = "zhangsan";this.password = "123";if(typeof Person.flag == "undefined"){alert("invoked");Person.prototype.getInfo = function(){alert(this.username + ", " + this.password);}Person.flag = true;}}var p = new Person();var p2 = new Person();p.getInfo();p2.getInfo();

 

Inheritance in JavaScript.

1) object impersonating

// Inherit the first method: the object impersonates function parent (username) {This. username = username; this. sayhello = function () {alert (this. username) ;}} function Child (username, password) {// The following three lines of code are the most critical code: This. method = parent; this. method (username); Delete this. method; this. password = password; this. sayworld = function () {alert (this. password) ;}} var parent = new parent ("zhangsan"); var child = new child ("Lisi", "1234"); parent. sayhello (); child. sayhello (); child. sayworld ();

2) Call method. 

The call method is a method in the function object, so every function we define has this method. You can call the call method by using the function name. The first parameter of the call method is passed to this in the function, and is assigned to the parameters in the function one by one starting with 2nd parameters.

// Use the call method to inherit objects

Function parent (username) {This. Username = username;

This. sayhello = function () {alert (this. username );}}

Function child (username, password) {parent. Call (this, username );

This. Password = password;

This. sayworld = function () {alert (this. Password );}}

VaR parent = new parent ("zhangsan ");

VaR child = new child ("Lisi", "123 ");

Parent. sayhello ();

Child. sayhello (); Child. sayworld ();

 

 

3) apply Method

// Use the apply method to implement Object Inheritance function parent (username) {This. username = username; this. sayhello = function () {alert (this. username) ;}} function Child (username, password) {parent. apply (this, new array (username); this. password = password; this. sayworld = function () {alert (this. password) ;}} var parent = new parent ("zhangsan"); var child = new child ("Lisi", "123"); parent. sayhello (); child. sayhello (); child. sayworld ();

 

4) prototype (parameters cannot be passed to the constructor)

// Use prototype chain to implement Object Inheritance function parent () {} parent. prototype. hello = "hello"; parent. prototype. sayhello = function () {alert (this. hello);} function Child () {} child. prototype = new parent (); child. prototype. world = "world"; child. prototype. sayworld = function () {alert (this. world);} var child = new child (); child. sayhello (); child. sayworld ();

5) hybrid mode (recommended)

// Implement the Object Inheritance (recommended) function parent (Hello) {This. hello = Hello;} parent. prototype. sayhello = function () {alert (this. hello);} function Child (Hello, world) {parent. call (this, hello); this. world = World;} child. prototype = new parent (); child. prototype. sayworld = function () {alert (this. world);} var child = new child ("hello", "world"); child. sayhello (); child. sayworld ();

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.