javascript--继承&多态

来源:互联网 发布:淘宝美女口水 编辑:程序博客网 时间:2024/06/05 14:51



<script type="text/javascript">/*之前说到 php 类和对象 js 的对象 php只需要类继承了 new个对象也是继承了js则是通过原型继承 */// 爸爸function gorila(){this.eat = function(){alert('amuamu');}}// 儿子function human(){}// 人的抽象函数就向猩猩对象学到了东西human.prototype = new gorila();var man = new human();man.eat();// man在自己的函数里没找到就到原型对象去找console.log(man);/*得到__proto__是个对象也就是new gorila指向其原型对象*/var gorila = new gorila();// 就是这个new gorila();console.log(gorila);


多态

<script type="text/javascript">function Dog(){this.bark = null;}function hashiqi(){this.bark = function(){alert('ashash');}}function samo(){this.bark = function(){alert('sssss');}}hashiqi.prototype = samo.prototype = new Dog();var h = new hashiqi();var s =  new samo();function test(dog){dog.bark();} test(h);test(s);/*静态方法 不需要new对象来调用*/samo.jtff = function(){alert('heihei');}samo.jtff();/*jtff方法是属于函数本身的,和返回的对象没关系;而函数内的bark方法要new对象才能调用*//*jQuery 静态 先给script加上src$.sing = funcion(){alert()*/</script>


0 0