[Douglas crockford js 类 继承模拟

Html:

<!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>继承测试</title>

</head>

<div id="divtest" ></div>

<body>

</body>

<script language="javascript" type="text/javascript" src="../js/inherit.js"></script>

</html>

---------------JS


// JavaScript Document

Function.prototype.method= function(name,func)

{

this.prototype[name]=func;

return this;

}

Function.method('inherit',function(parent){

  

var depth =0;

var proto = this.prototype = new parent();

this.method('uber',function uber(name){

 

var func;

var ret;

var v = parent.prototype;

if(depth)

{

for(var i=d;i>0;i+=1)

{

v = v.constructor.prototype;

}

func = v[name];

}else

{

func = proto[name];

if(func == this[name])

{

func = v[name];

}

}

depth +=1;

ret = func.apply(this,Array.prototype.slice.apply(arguments,[1]));

depth -= 1;

return ret;

});

return this;

  

});

Function.method('swiss',function(parent){

for(var i=0;i<arguments.length;i+=1)

{

var name = arguments[i];

this.prototype[name] = parent.prototype[name];

}  

return this;

}

);

///////////////////////////test ....

function Person(name)

{

this.name = name;

}

Person.method('getName',function (){

return this.name;

});


function User(name,password)

{

this.name = name;

this.password = password;

}

User.inherit(Person);


User.method("getPassword",function () {

return this.password;

});


User.method('getName',function(){

return "My Name is:" + this.name;   

});


var person1 = new Person("zhuxuegang1");

var user1 = new User("zhuxueganguser.","bqhtest..");



var divtest = document.getElementById("divtest");



divtest.innerHTML = user1.getPassword();


你可能感兴趣的:([Douglas crockford js 类 继承模拟)