JS-Implementing Object-oriented

Source: Internet
Author: User

<title>js– implementing Object-oriented</title> js– implementing object-oriented table of Contents
    • Public and private
    • Inherited
      • Inheriting through prototypes
      • Object Impersonation
      • Object replication
      • Implementing object Inheritance through Call/apply
    • Prototype
    • TODO Factory method
Public and private
function  Person() {var name="ABC";// Private    var  Age= 100;// Private     This. Show =function() {// public methods, you can only do this if you want to access private variablesWindow.alert (' name = '+ name); };function F() {Alert (' This is a private method '); }}p.new_func =function() {document.write (' I want to access to private FAIL '); alert (p.name);// undefined};p. New_func ();
Inheritance inherits through prototypes
function Tiger () {}functioncat() {This    function () {        alert (' cat Cat ')}    }} Tiger. prototype New Cat (); var T1 New Tiger (); var C1 New Cat (); t1.climb ();
Object Impersonation
function Animal(name, Age) { This. name = name; This. Age = Age;}function Dog(name, Age) { This. Animal = animal; This. Animal (name, age);}var Dog=New Dog(' a dog ', 2);d ocument.write (dog.name);if(Doginstanceof Animal) {alert (' Dog is an example of Animal ');// is a}
Object replication
function Cat() { This. Climb =function() {Alert (' Cat cat '); }}function Tiger() { This. Extend =function(Parent) { for(var Key inchParent) { This[Key] = Parent[key]; }    }}var T1=New Tiger(); T1.extend (New Cat()); t1.climb ();
Implementing object Inheritance through Call/apply
function Stu(name, Age) { This. name = name; This. Age = Age; This. Show =function() {Alert ( This. Name +' age is '+ This. age); }}function Midstu(name, Age) {Stu.call ( This, name, age);}var m=New Midstu(' One ', m.show);
function Cat() { This. Climb =function() {Alert (' Cat cat '); }}function Tiger() { This. parent = cat; This. parent.apply ( This,arguments);Delete  This. Parent;}var T1=New Tiger();var C1=New Cat(); t1.climb ();
function Dog() {Dog.prototype. Shout =function() {document.write ("Wang Wang Wang"); };}var Dog=New Dog();d og.shout ();// add a Find method to the arrayArray.prototype. Find =function(Val) {var Len= This. length; for(var I= 0; i < Len; i++) {if(val = = This[i]) {returnI }    }};
Prototype

The benefits of prototyping: Creating new methods, new properties, saving memory is not repeated

function Dog(name) { This. name = name; This. Func =function() {Alert (' .... '); };}var Dog1=New Dog(' AA ');var dog2=New Dog(' BB ');if(Dog1.func = = Dog2.func) {// Output Not EqualAlert' equal ');}Else{Alert (' Not equal ');} Dog.prototype. Func2 =function() {Alert (' New fun ');};var Dog3=New Dog(' AA ');var Dog4=New Dog(' BB ');if(Dog3.func2 = = DOG4.FUNC2) {// Output EqualAlert' equal ');}Else{Alert (' Not equal ');}
TODOFactory method
This can be used if the object is only one, not inherited ... (Factory method) var P New Object ();p. Name = .... P.age = ...

JS-Implementing Object-oriented

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.