Best practices for javascript simulation and javascript Best Practices
1: how to simulate a class
Define a class and a new class object in the sencha touch2 series.
Ext. define ("Animal", {config: {name: null}, constructor: function (config) {this. initConfig (config) ;}, speak: function () {console. log ('What do data') ;}}) var my = Ext. create ("Animal", {name: "bb"}) my. speak ();
In the code above, the constructor will be automatically called during create, and then initialize the properties configured for the config object. Constructor is like a constructor in a plane object ......
I will simulate it below
// In sencha, a new object uploads two parameters Ext. create ("Animal", {name: "bb"}) // The namespace of sencha is not simulated here, therefore, when a configuration object is generated, you can extract the namespace (mss) and namespace (new mss) from the configuration object. define ({}); var mss ={} // create a namespace mss. define = function (config) {if (typeof config! = 'Object') {console. log ('parameter error'); return;} var interface = function () {// when the new define returns the function, the atrr and initthis will be automatically executed. 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 = 'auto';}, 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.1 s]
In this way, we simulate the following: define a class, and then call its method after new;
2: How to inherit a class
First, let's take a look at the inheritance of the sencha touch2 series.
Ext. define ("Person", {extend: "Animal", speak: function () {console. log ('I am a personal ');}})
Add an extend property.
The following is a simulation in mss. define.
// In sencha, a new object uploads two parameters Ext. create ("Animal", {name: "bb"}) // The namespace of sencha is not simulated here, therefore, when a configuration object is generated, you can extract the namespace (mss) and namespace (new mss) from the configuration object. define ({}); var _ mss ={} // create a namespace _ mss. define = function (parClass, curConfig) {// If sup is an object, it indicates this is a new class. // If sup is a function, indicates that this is an inheritance if (typeof parClass = 'object') {curConfig = parClass; parClass = function (){};} // define the return class // when the new define returns the function, the atrr and initvar interface = function () {this will be automatically executed. attr & this. attr (); this. init & this. init. apply (this, arguments);} // The return class inherits parClassinterface. prototype = new parClass (); // defines the basic method for the two initialization functions contained in the returned class // obtains the inherited init method and attr method // If the parClass has the init method, so nterface. prototype. init // and new parClass (). init is equal to 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 attribute for the returned class prototype. Note that the current attribute may be overwritten by the following method for (var I in curConfig) {curConfig. hasOwnProperty (I) & (interface. prototype [I] = curConfig [I]);} // if the current returned class has inherited init, rewrite this 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 returned class has inherited attr, rewrite attr or construct the method (New Class) interface for the first time. prototype. attr = function () {parAttr. call (this); curAttr. call (this);} // inherit the member attributes 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 = 'auto';}, 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.1 s]
Call implementation inheritance
interface.prototype.attr = function() {parAttr.call(this);curAttr.call(this);}
Print this code on the Chorome console to explain it.
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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.