ExtJS5.1学习笔记3——自定义类的使用

代码如下:

<script type="text/javascript">
	Ext.define('Person', {
		name: '',
		age: 0,
		say: function(){
			Ext.Msg.alert(this.name + ' says: ', 'my name is ' + this.name + ', i am ' + this.age + ' years old.');
		},
		constructor: function(name, age){
			this.name = name;
			this.age = age;
		}
	});
	Ext.onReady(function(){
		var person = new Person('Tom', 24);
		person.say();
	});
</script>

Ext支持继承,下面编写一个Developer类,继承自 上面的Person类,代码如下:

Ext.define('Person', {
	name: '',
	age: 0,
	constructor: function(name, age){
		this.name = name;
		this.age =age;
	},
	speak : function(){
		Ext.Msg.alert(this.name + ' says: ', 'my name is ' + this.name + ', I am ' + this.age + ' years old.');
	}
});
Ext.define('Developer', {
	extend: 'Person',
	speak: function(){
		Ext.Msg.alert(this.name + ' says:', 'I am a Developer, my name is ' + this.name + ', I am ' + this.age + ' years old.');
	},
	constructor: function(){
		this.callParent(arguments);
	}
})
Ext.onReady(function(){
	// var person = new Person('Tom', 24);
	// person.speak();
	var developer = new Developer('Jack', 21);
	developer.speak();
});
浏览器中的执行结果如下图:

ExtJS5.1学习笔记3——自定义类的使用_第1张图片


你可能感兴趣的:(js,ext)