//封装
function _objEvent() {
this._callBack = "";//记录回调函数
this._setCallBack = function (callback)//保存回调寒素
{
if (typeof(callback) == "function")//判断回调函数
{
this._callBack = callback;//保存回调函数
}
}
this._onclick = function ()//执行保存的回调函数
{
if (typeof(this._callBack) == "function")//判断回调函数
{
this._callBack();//这里少了个()号执行回调
}
}
}
function showkey()//回调函数
{
alert("miaomiaomiao");
}
var obj1 = new _objEvent();//实例化
obj1._setCallBack(showkey);//给对象设置回调函数
obj1._onclick();//执行回调
//继承
function Person(name, age) {
this.name = name;
this.age = age;
this.sayname = function () {
alert("sayname");
}
}
// Person.prototype={
// hobby:function(){
// return "片片鱼";
//
// }
//
// }
Person.prototype.hobby = function () {
alert("吃片片鱼");
}
function DDD(name, age) {
//Person.call(this,name,age)
this.eat = function () {
//return "dddd";
//call(Presson.hobby());
}
}
DDD.prototype = new Person();
var lxy = new Person("罗雪燕", "21");
//alert(lxy.name+""+lxy.hobby());
var wx = new DDD("王潇", "22");
wx.sayname();
wx.hobby();
//最佳继承方式
function Person(name){
this.name = name;
}
Person.prototype.getName = function(){
return this.name;
}
function Teacher(name,age,qq){
this.age = age;
this.qq = qq;
Person.call(this,name);
}
Teacher.prototype.getAge = function(){
return this.age;
}
Teacher.prototype.getQq = function(){
return this.qq;
}
for(var i in Person.prototype){
Teacher.prototype[i] = Person.prototype[i];
}
var LiMing = new Teacher("LiMing",26);
var Zxue = new Teacher("Zxue",20);
console.log("Zxue=="+Zxue.getAge())
console.log("LiMing=="+LiMing.getName())
console.log("Zxue=="+Zxue.getName)
ES 6 继承
class Parent {
constructor(name) {
this.name = name;
}
doSomething() {
console.log('parent do something!');
}
sayName() {
console.log('parent name:', this.name);
}
}
class Child extends Parent {
constructor(name, parentName) {
super(parentName);
this.name = name;
}
sayName() {
console.log('child name:', this.name);
}
}
const child = new Child('son', 'father');
child.sayName(); // child name: son
child.doSomething(); // parent do something!
const parent = new Parent('father');
parent.sayName(); // parent name: father