Best practices for JavaScript simulation classes

Source: Internet
Author: User
Tags hasownproperty

1: How to emulate a class

Define a class and new object for this class in the Sencha TOUCH2 series

Ext.define ("Animal", {config: {name:null},constructor:function (config) {this.initconfig (config);},speak:function ( {Console.log (' Say Something ')}}) var my=ext.create ("Animal", {name: "BB"}) My.speak ();

In the above code, constructor is automatically called when Create, and then initializes the properties of the Config object configuration. Constructor is exactly like a constructor inside a polygon object ...

Let me simulate

In Sencha, the new object passed two parameters ext.create ("Animal", {name: "BB"})//The namespace of Sencha is not emulated here, so the object of this class is generated by passing a configuration object//namespace (MSS) And the name is extracted separately, new Mss.define ({}); var MSS = {}//Create a namespace Mss.define = function (config) {if (typeof config!== ' object ') {Console . log (' parameter error '); return;} var interface = function () {///when new define returns functions, it automatically executes ATRR and initthis.attr && this.attr (); This.init && This.init.apply (this, arguments);} for (var i in config) {config.hasownproperty (i) && (interface.prototype[i] = Config[i]);} return interface;} var car = mss.define ({attr:function () {this.type = ' car ';},init:function () {console.log (this.type);},speank:function () {Console.log (' I am ' + This.type);}}); var car1 = new Car (); Car1.speank ();
Output

Car I'm a car [finished in 0.1s]


This simulates: Define a class, then new comes out and calls its method;


2: How to inherit a class on this basis

First look at the inheritance of the Sencha TOUCH2 series


Ext.define ("person", {extend: "Animal", Speak:function () {console.log (' I am People ');})
Add one more attribute extend to get it done.

Let's simulate this in mss.define.

In Sencha, the new object passed two parameters ext.create ("Animal", {name: "BB"})//The namespace of Sencha is not emulated here, so the object of this class is generated by passing a configuration object//namespace (MSS) And the name is extracted separately, new Mss.define ({}), var _mss = {}//establishes a namespace _MSS. Define = function (Parclass, curconfig) {//If the SUP is an object indicating that this is a new class//If the SUP is a function that indicates that this is an inherited if (typeof parclass = = = ' Objec T ') {curconfig = Parclass;parclass = function () {};} Defines the return class//function that is returned by the new define, which automatically executes ATRR and Initvar interface = function () {this.attr && this.attr (); This.init && Amp This.init.apply (this, arguments);} The return class inherits Parclassinterface.prototype = new Parclass ();//defines the underlying method for the two initialization functions contained in the return class//Gets the inherited Init method and attr method// If the Init method exists for Parclass, then nterface.prototype.init//and new Parclass (). init equals var parinit = Interface.prototype.init | | function () {};var curinit = Curconfig.init | | function () {};var parattr = interface.prototype.attr | | function () {};var curattr = curconfig.attr | | function () {};//Initializes the current property for the return class prototype, which is noted here may be overridden by the following method for the (var i in Curconfig) {curconfig.hasownproperty (i) && ( Interface.prototype[i] = CUrconfig[i]);} If the current return class has inherited Init, override the method if (arguments.length && arguments[0].prototype && arguments[0]. Prototype.init = = = Parinit) {interface.prototype.init = function () {var scope = This;var args = [function () {parinit.apply (scope, arguments);}]; var slice = [].slice;curinit.apply (Scope, Args.concat (Slice.call (arguments)));}} If the current return class has inherited attr, overriding attr or the first construction modification method (new Class) interface.prototype.attr = function () {parattr.call (this); Curattr.call ( this);} Inherits the member property of the parent class for (Var i in Parclass) {parclass.hasownproperty (i) && (interface[i] = parclass[i]);} return interface;} var Car = _mss. Define ({attr:function () {this.type = ' car ';},init:function () {console.log (this.type);},speank:function () { Console.log (' I am ' + This.type ';}}); var car1 = _mss. Define (Car, {}) New Car1 (). Speank ();

Output

Car I'm a car [finished in 0.1s]

Inheritance for Call implementations

Interface.prototype.attr = function () {Parattr.call (this), Curattr.call (this);}

This code can be explained by printing this code in the Chorome console.

var _attr = function () {  THIS.A = 1;} var B = function () {  this.attr ();}; B.prototype.attr = function () {_attr.call (this);} Console.log (New B ()); Vm665:9 B {a:1}a:1__proto__: battr: () {_attr.call (this);} Constructor: () {__proto__: Object


For detailed call usage see: http://www.cnblogs.com/wangtao_20/archive/2011/01/01/1923918.html or http://uule.iteye.com/blog/1158829

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Best practices for JavaScript simulation classes

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.