Created
April 6, 2016 19:02
-
-
Save ChrisMoney/9b6fa6dee444b24b27a21bdf9d9440a9 to your computer and use it in GitHub Desktop.
ExtendJS code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<head> | |
<script src="http://cdn.extendjs.org/0.2.3/extend.min.js"></script> | |
... | |
</head> | |
//Create MyClass by extending Class. | |
var MyClass = Class.extend(function(){ | |
//Classes can have constructors | |
this.constructor = function(){ | |
//... | |
}; | |
}); | |
//---------Working with methods | |
var MyClass = Class.extend(function(){ | |
//This is a private method. | |
function privateMethod(){ | |
console.log("Private method executed"); | |
} | |
//this is a public method. | |
this.publicMethod = function(){ | |
console.log("Public method executed"); | |
}; | |
this.constructor = function(){ | |
//Private methods accessed inside the class instances. | |
privateMethod(); | |
}; | |
}); | |
//Public variables can be accessed as properties on class instances. | |
var instance = new MyClass(); | |
instance.publicMethod(); | |
//Outputs | |
//=> "Private method executed"; | |
//=> "Public method executed"; | |
console.log(new MyClass() instanceof MyClass); //=> "true" | |
//Working with variables | |
var MyClass = Class.extend(function(){ | |
//This is a private variable. | |
var privateVariable = "private variable"; | |
//this is a public variable. | |
this.publicVariable = "public variable"; | |
this.constructor = function(){ | |
//Private variables can only be accessed inside the class instances. | |
console.log(privateVariable); //=> "private variable"; | |
}; | |
}); | |
//Public variables can be accessed as properties on class instances. | |
var instance = new MyClass(); | |
console.log(instance.publicVariable); //=> "public variable" | |
//---------Extending your own classes | |
//Define MyClass | |
var MyClass = Class.extend(function(){ | |
//Declare public variable | |
this.variable = "public variable"; | |
}); | |
//MyOtherClass extends MyClass | |
var MyOtherClass = MyClass.extend(function(){ | |
}); | |
//----------The super constructor | |
//Define MyClass | |
var MyClass = Class.extend(function(){ | |
this.constructor = function(){ | |
//Output from original class constructor | |
console.log("MyClass constructor"); | |
}; | |
}); | |
//MyOtherClass extends MyClass | |
var MyOtherClass = MyClass.extend(function(){ | |
this.constructor = function(){ | |
//Execute the constructor of the class we extended. | |
this.super(); | |
//Output from extending class | |
console.log("MyOtherClass constructor"); | |
}; | |
}); | |
//Create instance of MyOtherClass | |
var instance1 = new MyOtherClass(); | |
//Outputs | |
//=> "MyClass constructor" | |
//=> "MyOtherClass constructor" | |
var instance = new MyOtherClass(); | |
console.log(instance.variable); //=> "public variable" | |
//-----------Method overloading | |
//Define MyClass | |
var MyClass = Class.extend(function(){ | |
//Decleare test method | |
this.testMethod = function(){ | |
console.log("MyClass test method"); | |
}; | |
}); | |
//MyOtherClass extends MyClass | |
var MyOtherClass = MyClass.extend(function(){ | |
//Overload test method | |
this.testMethod = function(){ | |
//Calling super instance of testMethod | |
this.super.testMethod(); | |
console.log("MyOtherClass test method"); | |
}; | |
}); | |
//Create instance of MyOtherClass | |
var instance = new MyOtherClass(); | |
//Call test method. | |
instance.testMethod(); | |
//Outputs | |
//=> "MyClass test method" | |
//=> "MyOtherClass test method" | |
//Advanced: Scope guarantee | |
//Define MyClass | |
var MyClass = Class.extend(function(){ | |
//Decleare test method | |
this.testMethod = function(){ | |
console.log( | |
"MyClass test method", | |
this instanceof MyClass | |
); | |
}; | |
}); | |
//Create instance of MyClass | |
var instance = new MyClass(); | |
//Call method of class such that scope would normally be global. | |
setTimeout(instance.testMethod,100); | |
//Output after 100ms | |
//=> "MyClass test method", true | |
//----------Advanced: The initializer method | |
var MyClass = Class.extend(function(){ | |
//initializer is executed before the constructor. | |
this.initializer = function(){ | |
//... | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment