类声明/继承/重写/命名空间

时间:2022-09-08 09:00:20
var Person = function(name) {
    this.name = name;
}  
Person.prototype = {
    word1 : "cccc",
     sayHello: function() {
         alert("hi, javaeye, I'm A " + this.name);
    }
}
//说明word1:"cccc",表示初始化类变量,它其实与this.name是一样的功能,反过来说初始化类可以用二种方法,只是从理论上word1表示类的属性,另一个表示方法


//继承
Developer = Ext.extend(Person, {
    constructor: function(name){
        this.name = name;
    },
    word : "cc",
    getName: function(){
        alert(this.name);
    }
})
var p = new Developer('Johnbb');
p.getName();
p.sayHello();
alert(p.word);
alert(p.word1);

//重写Person类
Ext.override(Person,{
    sayHello: function() {
         alert("hi, it's override " + this.word1);
    }
})
p.sayHello();

//类的命名空间
Ext.namespace('MyPerson');
MyPerson.Person = function(name) {
    this.name = name;
}
MyPerson.Person.prototype = {
    word1 : "cccc",
     sayHello: function() {
         alert("hi, javaeye, I'm A " + this.name);
    }
}
MyPerson.Person1 =Ext.extend(
    MyPerson.Person,
    {}
)
var p = new MyPerson.Person1('John');
p.sayHello();