【JavaScript 6連載】二、函式(工廠模式)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>02-工廠模式</title>
<script>
/*
// 函式在執行的時候,執行環境的作用域是當前函式的呼叫者
function testThis (){
console.log(this);
}
// window.testThis();
*/
var p1 = {name:'小明',age:18};
var p2 = {name:'小芳',age:18};
var p3 = {name:'小亮',age:18};
var p4 = {name:'小莉',age:18};
var p5 = {name:'小花',age:18};
var p6 = {name:'小剛',age:18};
function createStudent(name,age,sex,sid){
var obj = {
name:name,
age:age,
sex:sex,
sid:sid,
study:function (){
console.log(this.name + '好好學習,天天向上');
}
}
return obj;
}
var s1 = createStudent('小明',18,'b','20161001');
var s2 = createStudent('小芳',18,'g','20161002');
var s3 = createStudent('小亮',18,'b','20161003');
var s4 = createStudent('小莉',18,'g','20161004');
var s5 = createStudent('小剛',18,'b','20161005');
// 工廠模式的弊端,不能清楚的知道物件的具體型別
</script>
</head>
<body>
</body>
</html>