Object-oriented understanding of JavaScript in ExtJS

Source: Internet
Author: User
Tags aliases extend instance method

1: Support namespaces
<script type= "Text/javascript" >
Define a Namespace
Ext.namespace ("Ext.wentao");
To define a class on a namespace
Ext.wentao.helloworld = EXT.EMPTYFN;

Create an instance of a class
New Ext.wentao.helloworld ();
</script>

which
Ext.wentao.helloworld = EXT.EMPTYFN;

Equivalent to
Ext.wentao.helloworld = function () {};


2: Support class Instance properties
<script type= "Text/javascript" >
Ext.namespace ("Ext.wentao"); Customizing a Namespace
Ext.wentao.Person = EXT.EMPTYFN; Customizing a class on a namespace

Add a Name property to the custom class and assign a value
Ext.apply (Ext.wentao.Person.prototype, {
Name: "Liu Wentao"
})

var _person = new Ext.wentao.Person ()//Instantiate custom class
alert (_person.name);
_person.name = "John";//Modify Class Name property
alert (_person.name);
</script>


3: Support class instance method
<script type= "Text/javascript" >
Ext.namespace ("Ext.wentao"); Customizing a Namespace
Ext.wentao.Person = EXT.EMPTYFN; Customizing a class on a namespace

Demo class instance method
Ext.apply (Ext.wentao.Person.prototype, {
Name: "Liu Wentao",
Sex: "Male",
Print:function () {
Alert (String.Format ("name: {0}, Sex: {1}", This.name,this.sex));
}
})

var _person = new Ext.wentao.Person ()//Instantiate custom class
_person.print ();
</script>


4: Support class static method
<script type= "Text/javascript" >
Ext.namespace ("Ext.wentao"); Customizing a Namespace
Ext.wentao.Person = EXT.EMPTYFN; Customizing a class on a namespace

Demo class instance method
Ext.apply (Ext.wentao.Person.prototype, {
Name: "Liu Wentao",
Sex: "Male",
Print:function () {
Alert (String.Format ("name: {0}, Sex: {1}", This.name,this.sex));
}
})

Demo Class static methods
Ext.wentao.Person.print = function (_name,_sex) {
var _person = new Ext.wentao.Person ();
_person.name = _name;
_person.sex = _sex;
_person.print (); The class instance method is called here, and the print above is a class static method
}

Ext.wentao.Person.print ("John", "female"); Calling class static methods
</script>


5: Supporting the construction method
<script type= "Text/javascript" >
Ext.namespace ("Ext.wentao"); Customizing a Namespace

Construction method
Ext.wentao.Person = function (_cfg) {
Ext.apply (THIS,_CFG);
}

Demo class instance method
Ext.apply (Ext.wentao.Person.prototype, {
Print:function () {
Alert (String.Format ("name: {0}, Sex: {1}", This.name,this.sex));
}
})

Demo Class static methods
Ext.wentao.Person.print = function (_name,_sex) {
var _person = new Ext.wentao.Person ({name:_name,sex:_sex});
_person.print (); The class instance method is called here, and the print above is a class static method
}

Ext.wentao.Person.print ("John", "female"); Calling class static methods
</script>


6: Support class inheritance
<script type= "Text/javascript" >
Ext.namespace ("Ext.wentao"); Customizing a Namespace

Parent class *********************
Construction method
Ext.wentao.Person = function (_cfg) {
Ext.apply (THIS,_CFG);
}

Demo class instance method
Ext.apply (Ext.wentao.Person.prototype, {
Job: "None",
Print:function () {
Alert (String.Format ("name: {0}, Sex: {1}, role: {2}", This.name,this.sex,this.job));
}
})

Sub Class 1*********************

Ext.wentao.Student = function (_cfg) {
Ext.apply (THIS,_CFG);
}

Ext.extend (ext.wentao.student,ext.wentao.person,{
Job: "Student"
})

var _student = new Ext.wentao.Student ({name: "John", Sex: "female"});
_student.print (); Calling the parent class method
</script>


7: Support class instance method overrides
<script type= "Text/javascript" >
Ext.namespace ("Ext.wentao"); Customizing a Namespace

Parent class *********************
Construction method
Ext.wentao.Person = function (_cfg) {
Ext.apply (THIS,_CFG);
}

Demo class instance method
Ext.apply (Ext.wentao.Person.prototype, {
Job: "None",
Print:function () {
Alert (String.Format ("name: {0}, Sex: {1}, role: {2}", This.name,this.sex,this.job));
}
})

Sub Class 1*********************

Ext.wentao.Student = function (_cfg) {
Ext.apply (THIS,_CFG);
}

Overriding an instance method of a parent class
Ext.extend (ext.wentao.student,ext.wentao.person,{
Job: "Student",
Print:function () {
Alert (String.Format ("{0} is a {1}{2}", This.name,this.sex,this.job));
}
})

var _student = new Ext.wentao.Student ({name: "John", Sex: "female"});
_student.print (); Calling the parent class method
</script>

8: namespace alias support
<script type= "Text/javascript" >
Ext.namespace ("Ext.wentao"); Customizing a Namespace

Wt = Ext.wentao; Aliases for namespaces

Parent class *********************
Construction method
Wt.person = function (_cfg) {
Ext.apply (THIS,_CFG);
}

Demo class instance method
Ext.apply (Wt.Person.prototype, {
Job: "None",
Print:function () {
Alert (String.Format ("name: {0}, Sex: {1}, role: {2}", This.name,this.sex,this.job));
}
})

Sub Class 1*********************

Wt.student = function (_cfg) {
Ext.apply (THIS,_CFG);
}

Overriding an instance method of a parent class
Ext.extend (wt.student,ext.wentao.person,{
Job: "Student",
Print:function () {
Alert (String.Format ("{0} is a {1}{2}", This.name,this.sex,this.job));
}
})

var _student = new Wt.student ({name: "Zhang Q III", Sex: "female"});
_student.print (); Calling the parent class method
</script>

9: Support class Alias
<script type= "Text/javascript" >
Ext.namespace ("Ext.wentao"); Customizing a Namespace

Wt = Ext.wentao; Aliases for namespaces

Parent class *********************
Construction method
Wt.person = function (_cfg) {
Ext.apply (THIS,_CFG);
}

PN = Wt.person; Class Alias

Demo class instance method
Ext.apply (Pn.prototype, {
Job: "None",
Print:function () {
Alert (String.Format ("name: {0}, Sex: {1}, role: {2}", This.name,this.sex,this.job));
}
})

Sub Class 1*********************

Wt.student = function (_cfg) {
Ext.apply (THIS,_CFG);
}

ST = wt.student;

Overriding an instance method of a parent class
Ext.extend (st,pn,{
Job: "Student",
Print:function () {
Alert (String.Format ("{0} is a {1}{2}", This.name,this.sex,this.job));
}
})

var _student = new ST ({name: "Zhang Q III", Sex: "female"});
_student.print (); Calling the parent class method
</script>

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.