ExtJS basic knowledge of object-oriented programming

Source: Internet
Author: User
Tags instance method

1: Support namespaces

<script type="text/javascript">
// 定义 一个命名空间
Ext.namespace("Ext.wentao");
// 在命名空间上定义一个类
Ext.wentao.helloworld = Ext.emptyFn;
// 创建一个类的实例
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"); // 自定义一个命名空间
Ext.wentao.Person = Ext.emptyFn; // 在 命名空间上自定义一个类
// 为自定义的类 增加一个 name 属性,并赋值
Ext.apply (Ext.wentao.Person.prototype, {
name : "刘文涛"
});

var _person = new Ext.wentao.Person();// 实例化 自定义类
alert(_person.name);
_person.name = "张三";// 修改类name属性
alert(_person.name);

</script>

3: Support class instance method

<script type="text/javascript">
Ext.namespace("Ext.wentao"); // 自定义 一个命名空间
Ext.wentao.Person = Ext.emptyFn; // 在命名空间上自定义一个类
// 演示 类实例方法
Ext.apply(Ext.wentao.Person.prototype, {
name : "刘文涛",
sex : "男",
print : function() {
alert(String.format("姓名:{0}, 性别:{1}", this.name, this.sex));
}
});

var _person = new Ext.wentao.Person();// 实例化 自定义类
_person.print();


</script>

4: Support class static method

<script type="text/javascript">

Ext.namespace("Ext.wentao"); // 自定义一个命名空间
Ext.wentao.Person = Ext.emptyFn; // 在命名空间上自定义一个类

// 演示类实例方法
Ext.apply(Ext.wentao.Person.prototype, {
name : " 刘文涛",
sex : "男",
print : function() {
alert (String.format("姓名:{0},性别:{1}", this.name, this.sex));
}
});

// 演示 类静态方法
Ext.wentao.Person.print = function(_name, _sex) {
var _person = new Ext.wentao.Person();
_person.name = _name;
_person.sex = _sex;
_person.print(); // 此处调用类 实例方法,上面print是类 静态方法
};

Ext.wentao.Person.print("张三", "女"); // 调用类 静态方法

</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.