- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Untitled Document</title>
- </head>
- <body>
- <script type="text/javascript">
- Object.extend = function(destination, source){ //为Object对象添加一个 “对象扩展” 的静态方法
- for(var p in source){
- destination[p] = source[p]
- }
- return destination;
- }
- Object.prototype.extend = function(object){
- return Object.extend.apply(this, [this,object]) //利用Object类的静态方法为每个object对象添加 “扩展”的方法
- }
- function class1(){
- this.name = 'jack'
- }
- class1.prototype = {
- method : function(){
- alert(this.name);
- }
- }
- function class2(){
- this.name = 'mike'
- }
- class2.prototype = (new class1()).extend({
- method2 : function(){
- alert('hello ' + this.name)
- }
- })
- var a = {
- name : 'sco'
- }
- var b = {};
- b.extend(a);
- alert(b.name); // sco
- var obj = new class2();
- obj.method(); // mike
- obj.method2(); // hello mike
- alert(obj instanceof class2); // true
- alert(obj instanceof class1); // true
- </script>
- </body>
- </html>
简单的javascript 继承机制
最新推荐文章于 2025-06-15 14:00:34 发布