Javascript Object-Oriented Programming-writing of Object Inheritance

Source: Internet
Author: User

Object with attributes and methods. This is what everyone has heard. Object-oriented features include closed, inherited, and polymorphism. Today, I want to summarize what I just learned. I just wrote down the tip of the iceberg and hope it can be improved.

I believe everyone is familiar with Object Inheritance. You can use the following methods: Object impersonating, call () [apply ()], prototype, and hybrid. Here, I think the mixed method is better. Let's first review the call () method:

 

function useCall(a,b){  this.a = a;  this.b = b;  this.say=function(){    alert("I'm "+this.a+" You're"+this.b);  } } function callThefunction (){    var args = arguments;    useCall.call(this,args[0],args[1]); //  useCall.apply(this,arguments);  }var testCall1 =new useCall("Not YY","Not TT");testCall1.say();var testCall2 = new callThefunction("YY","TT");testCall2.say();

 

Prototype:

 

Function car (price) {this. price = price;} car. prototype = {sayPrice: function () {console. log ("Price is" + this. price);} // console. log () See Firebug API} function toyCar (price) {this. price = price;} toyCar. prototype = new car () var oCar = new car ("100 W"); oCar. sayPrice (); var oCar2 = new toyCar ("10CNY"); oCar2.sayPrice ();

 

Hybrid mode:

 

Function house (size, price) {this. size = size; this. price = price;} house. prototype. showArea = function () {console. log ("area is" + this. size);} house. prototype. sayPrice = function () {console. log ("price is" + this. price);} function maofan (size, price) {house. call (this, size, price);} maofan. prototype = new house (); var newmaofan = new maofan ("20 Square meters", "1000CNY"); newmaofan. showArea ();

 

Another method of mixed writing:

 

Function house (size, price) {this. size = size; this. price = price;} house. prototype = {showArea: function () {console. log ("area is" + this. size) ;}, sayPrice: function () {console. log ("price is" + this. price) ;}} function maofan (size, price) {house. call (this, size, price);} maofan. prototype = new house (); var newmaofan = new maofan ("20 Square meters", "1000CNY"); newmaofan. showArea (); newmaofan. sayPrice ();

 

Try to append a method to the inherited object:

 

Function house (size, price) {this. size = size; this. price = price;} house. prototype = {showArea: function () {console. log ("area is" + this. size) ;}, sayPrice: function () {console. log ("price is" + this. price) ;}} function maofan (size, price) {house. call (this, size, price);} maofan. prototype = new house (); maofan. prototype. sayid = function (id) {// Add a method after inheritance. Otherwise, an error occurs and the method this cannot be found. id = id; console. log ("ID is:" + this. id);} var newmaofan = new maofan (); newmaofan. sayid ("888"); // Output ID: 888

 

You can give it a try in a few examples. I never thought that experts would read these articles, but I just got started. So don't be surprised if the writing is not good. All these demos have to be tried, and the information you have checked should be familiar with the JavaScript inheritance. It is worth noting that there are many writing methods that we have never seen before. I don't often use it like a mixed method. In my opinion, it is an array, which will be much better understood. If you look at the examples too much, you will not be dizzy if you look at the difficult examples. However, I still see that the long one will be dizzy and the _ string _ will appear. Although there is nothing remarkable, I don't want to read it if I don't understand it. So it's not that easy to learn.

I forgot one point. When I wrote call (), I first wrote xxx. call (this, a, B). But I reported an error. Why? Thank you for calling (thisobj, args [0]…), Big Brother Sima xianxian In the JS group. If the parameter is written as I did, it is not only the parameter is incorrect, but also the parameter is not used properly.

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.