JavaScriptOOP

function BaseConstructor1(name){
    var varPriv01 = "hello varPriv01";
    var funcPriv01 = function(){
        alert("hello funcPriv01");
    };
    this.varPub01 = "hello varPriv01";
    this.funcPub01 = function(){
        alert("hello funPub01"+name);
    }
}
BaseConstructor1.staticFunc1 = function(){//静态方法
    alert("hello staticFunc1");
}
function SubConstructor1(name){//继承
    BaseConstructor1.call(this,name);
}
//---------------------原型继承------------------
function Animal(){
    this.eat = function(){
        alert("animal eat!");
    }
    this.move = function(){
        alert("animal move!");
    }
}
function Dog(){
    this.bark = function(){
        alert("Dog bark!");
    }
    this.run = function () {
        alert("Dog run!");
    }
}
 
 
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script src="JSOOP.js"></script>
<body>
    <script>
        // var base001 = new BaseConstructor1("zhangsan");
        // alert(base001.varPub01);
        // base001.funcPub01();
        //
        // var  sub01 = new SubConstructor1("lisi");
        // sub01.funcPub01();
        //BaseConstructor1.staticFunc1();
        //alert(base001.constructor == BaseConstructor1);

        //Dog.prototype = new Animal(); //继承Animal
        Dog.prototype.eat = function(){//重写,使用prototype目的是节约内存
            alert("parent eat!");
        }

        var wangcai = new Dog();//容易消耗内存

        // alert(wangcai.__proto__ === Dog.prototype);
        // alert(wangcai.__proto__.__proto__ === Animal.prototype);
        // wangcai.bark();
        // wangcai.run();
         wangcai.eat();
        // wangcai.move();
        //利用Json-》面向对象,只能当一个普通对象,不能new
        var monkey = {
            jump:function(){
                alert("monkey jump");
            },
            run:function(){
                alert("monkey run");
            }
        }
        monkey.jump();
        monkey.run();

        var myDate = new Date();
        myDate.setFullYear(2010,09,10);
        var date = new Date();
        if(myDate > date){
            alert("my");
        }
    </script>
</body>
</html>
 
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值