JavaScriptOOP

来源:互联网 发布:智慧城市优化公共服务 编辑:程序博客网 时间:2024/06/06 19:50
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>
 
原创粉丝点击