<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>工厂模式</title>
</head>
<body>
<script>
function createPerson(name,age){
var obj = new Object ();
obj.uname = name;
obj.age =age;
obj.showPerson= function(){
console.log(this.age);
console.log(this.uname);
}
return obj;
}
var man = new createPerson('ajia',23);
man.showPerson()
</script>
</body>
</html>
<script>
function createPerson(name,age){
this.uname = name;
this.age = age;
this.showPerson = function(){
console.log(this.uname);
console.log(this.age)
}
}
var obj = new createPerson('zjs',23);
obj.showPerson();
</script>
<meta charset="utf-8">
<script>
function createPeson(name,age){
this.name = name;
this.age = age;
this.showMf = function(){
this.name= 'aaa---';
console.log(this.name)
}
}
createPeson.prototype.showPerson = function(){
console.log(this.name);
console.log(this.age);
}
var obj = new createPeson('ajia',23);
obj.showPerson();
obj.showMf();
</script>
<meta charset="utf-8">
<script>
function createPerson(name,age){
this.name = name;
this.age = age;
//动态创建一个方法
if(typeof createPerson.doWork == 'undefined'){
createPerson.prototype.doWork = function(){
console.log(this.name+'---'+this.age+'---'+'在学习');
}
createPerson.doWork = true;
}
}
var person = new createPerson('zjs',23);
person.doWork();
console.log(createPerson.doWork)
var person1 = new createPerson('ajia',33);
person1.doWork();
console.log(createPerson.doWork)
</script>
<meta charset="utf8">
<script>
//继承1
function Parent(name){
this.name=name;
this.sayParent=function(){
console.log("Parent:"+this.name);
}
}
function Child(name,age){
this.tempMethod=Parent;
this.tempMethod(name);
this.age=age;
this.sayChild=function(){
console.log("Child:"+this.name+"age:"+this.age);
}
}
var c = new Child('zjs',23);
c.sayChild();
c.sayParent()
</script>
<meta charset="utf-8">
<script>
// 继承2
function Person(name,age,love){
this.name=name;
this.age=age;
this.love=love;
this.say=function say(){
console.log("姓名:"+name);
}
}
//这里是call方法
function student(name,age){
Person.call(this,name,age);
}
// 这里是apply方法
function teach(name,love){
// Person.apply(this,[name,love]);
Person.apply(this,arguments);
}
//call与aplly的异同:
//1,第一个参数this都一样,指当前对象
//2,第二个参数不一样:call的是一个个的参数列表;apply的是一个数组(arguments也可以)
var t = new teach('mayun','baseketball');
t.say()
// var p = new Person('zjs',23,'nba');
// var s = new student('ajia',28);
// s.say();
</script>
<meta charset="utf-8">
<script>
//继承3
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype.sayHello=function(){
console.log("使用原型得到Name:"+this.name);
}
function Student(){}
Student.prototype=new Person("洪如彤",21);
var stu=new Student();
Student.prototype.grade=5;
Student.prototype.intr=function(){
console.log(this.grade);
}
stu.sayHello();//输出:使用原型得到Name:洪如彤
stu.intr();//输出:5
</script>