Last active
August 29, 2015 14:03
-
-
Save hehongwei44/b5623dda5b43a6731b55 to your computer and use it in GitHub Desktop.
通过把父类的对象实例赋值给子类来实现原型式集成
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title>js继承</title> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
function Person(name) { | |
this.name = name; | |
} | |
Person.prototype.getName = function(){ | |
return this.name; | |
} | |
function User (name,password){ | |
this.name = name; | |
this.password = password; | |
} | |
User.prototype = new Person(); | |
User.prototype.getPassword = function(){ | |
return this.password; | |
} | |
var user = new User("john","123456"); | |
console.log(user.getName()); | |
console.log(user.getPassword()); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment