JavaScript Object-oriented and prototype

Source: Internet
Author: User

/*Factory mode function CreateObject (name,age) {var obj = new Object ();//Create a new object obj.name=name;//the properties of the new object obj.age=age;    Obj.run=function () {//New object method return this.name+ "," +this.age+ "... running.";} Return obj;//returns the newly created object reference}var Box1 = CreateObject ("Carl"), var Box2 = CreateObject ("Zhang"); Alert (Box1.run ()); alert    (Box2.run ());//constructor function Box (name,age) {this.name=name;//The properties of the new object this.age=age; This.run=function () {//New object method return this.name+ "," +this.age+ "... running.";}} var box1 = new Box ("Carl", 20);//Object instantiation var box2 = new Box ("Zhang"), Alert (Box1.run ()); alert (Box2.run ()); Alert (box1    Instanceof object);//constructor function box (name,age) {this.name=name;//The properties of the new object this.age=age; This.run=function () {//New object method return this.name+ "," +this.age+ "... running.";}} var box1 = new Box ("Carl", 20);//Object instantiation var box2 = new Box ("Zhang"), Alert (Box1.run ()); alert (Box2.run ()); Alert (box1 Instanceof object);//constructor function Box (name,age) {this.name=name;//New ObjectAttribute This.age=age; This.run=function () {//New object method return this.name+ "," +this.age+ "... running.";}} var o = new Object (); Box.call (O, "Carl");//call object impersonating alert (O.run ())//Constructor function Box (name,age) {this.name=name;//New object's Properties This.age=age    ; This.run=new Function ("Return this.name+this.age+").    Running ... '); }var o = new Object (); Box.call (O, "Carl");//call object impersonating alert (O.run ());//constructor function Box (name) {this.name=name;//Instance property} Box.prototype.name= "Zhang";//Prototype Properties box.prototype.age=25;//prototype Properties Box.prototype.run=function () {//prototype method return THIS.name + "," +this.age+ "... running.";} In the constructor body called instance properties, each new object, its instance properties address is not the same//prototype is a reference, point to an object, the property created by prototype, called the prototype property, each time the new object's prototype property address is the same. var box1 = new Box ("Carl");//Object Instantiation alert (Box1.run ()); alert (box1.constructor);//constructor, returns the constructor itself alert ( Box1.hasownproperty ("name"));//Determines whether the instance property contains the name attribute delete box1.name;//Delete Instance property alert ("name" in Box1); True to determine whether the instance attribute is present or the prototype attribute//constructor function Box (name) {this.name=name;//instance property}box.prototype={Name: "ZhaNg ",//Prototype attribute age:25,//prototype attribute run:function () {//prototype method return this.name+", "+this.age+" ... running. ";}} var box = new box (), Alert (box.constructor),//ojbect//constructor function box (name) {this.name=name;//instance property}box.prototype={C Onstructor:box, Name: "Zhang",//Prototype attribute age:25,//prototype attribute run:function () {//prototype method return this.name+ "," +this.age+ "... . running. ";}} var box = new box (), Alert (box.constructor),//box//constructor function box (name) {this.name=name;//instance property}box.prototype={Cons Tructor:box, Name: "Zhang",//Prototype attribute age:25,//prototype attribute run:function () {//prototype method return this.name+ "," +this.age+ "... run Ning. ";}} Rewrite prototype box.prototyp={name: "Zhang"}var box = new box (), Alert (Box.run ());//errorvar Box=[2,1,29,3,0,13];alert (Box.sort ()); alert (Array.prototype.sort);//See if sort is the prototype function of Array alert (String.prototype.addstring); String.prototype.addstring=function () {return this+ "... I am the extension of the method ";} Alert ("Test addstring". addstring ());//dynamic prototype mode//constructor function Box (name,age) {this.name=name;//instance property   This.age=age;    Encapsulate the prototype into the constructor Box.prototype.run=function () {return this.name+this.age+ "running ..."; }}var box1= New Box ("Carl"), var box2= new box ("Zhang"), Alert (Box1.run ()), Alert (Box2.run ());//constructor function Box (            Name,age) {this.name=name;//instance property this.age=age;        Encapsulating the prototype into the constructor if (typeof this.run!= ' function ') {//Plus this paragraph allows the following prototype to initialize only one alert ("Initialization start");        Box.prototype.run=function () {return this.name+this.age+ "running ...";            } alert ("Initialization End"); }}var box1= New Box ("Carl"), var box2= new box ("Zhang", 20);//inheritance, the function Box () {//inherited by the prototype chain is called the superclass (parent class, base class) THIS.name = "Carl";} function Desk () {//inherited functions are called sub-types (subclasses, derived classes) this.age=100;} Through the prototype chain inheritance, the superclass instantiation of the object instance, assigned to the subtype of the prototype attribute desk.prototype=new box ();//new box () will give the information in the box structure and the information in the prototype to Deskvar desk = new Desk ( ); alert (desk.name);*//*//1. Prototype chain inheritance 2. Borrowing constructor inheritance (object impersonation Inheritance) 3. Combinatorial inheritance (combined with the first two)//4. prototype inheritance/temporary relay function obj (o) {function F () {};    F.prototype=o; return new F (); box={Name: "Carl", Age:20, family:["elder brother", "elder sister", "brother"]};var box1 = obj (box), alert (box1.family); Box1.family.push ("Sister") ; alert (box1.family); var box2 = obj (box); alert (box2.family) ;*///Temporary transfer functionfunctionobj (o) {functionF () {}; F.prototype=o; return NewF ();}//Parasitic FunctionsfunctionCreate (box,desk) {varf=obj (box.prototype); F.constructor=desk;//adjusting the prototype construction pointersDesk.prototype=F;}functionBox (name,age) { This. name=name;  This. age=Age ;} Box.prototype.run=function(){    return  This. name+ This. age+ "Running ...";}functionDesk (name,age) {Box.call ( This, name,age);//Object Impersonation}//inheritance through parasitic combination inheritanceCreate (Box,desk);//This sentence is used instead of desk.prototype=new Box ();varDesk =NewDesk ("Carl", 20Alert ( Desk.run ()); alert (desk.constructor); alert (desk.constructor.prototype); alert (desk.__proto__===desk.constructor.prototype);

Note: This article is part of the code from the Li Tinhui teacher's JavaScript tutorial

JavaScript Object-oriented and prototype

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.