构造函数 闭包形成私有化变量

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>

<script type="text/javascript">
// 1. 构造函数生产对象
function Person() {
this.name = 'abc';
this.age = 123;
}
// 在new Person() 在构造函数内部隐式的
// function Person() {
// var this = {}
// this.name = 'abc';
// this.age = 123;
// return this
// }
var person = new Person();
console.log(person);

//2. 函数执行
function Person() {
this.name = 'abc';
this.age = 123;
}
Person();
</script>



<script>
// 3.闭包形成私有化变量
function Person(name){
// var this = {
// makeMoney:function(){}
// offer:function(){}
// }
var money = 100;
this.name = name;
this.makeMoney = function(){
money++;
}
this.offer = function(){
money--;
}
// return this
}
var person = new Person();
</script>


<script>
// 继承应用
var inherit = (function(){
var F = function(){}
// Origin 前辈 Target晚辈
return function(Target,Origin){
F.prototype = Origin.prototype;
Target.prototype = new F();
}
}())
</script>
</body>
</html>

posted @ 2020-09-16 21:44  Smile*^  阅读(144)  评论(0编辑  收藏  举报