<!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>

  

最新文章

  1. PHP的学习--使用phar打包
  2. android开发 兵器
  3. ubuntu14.04 python自带版本升级
  4. MVVM架构~knockoutjs系列之扩展ajax验证~验证输入数据是否与后台数据相等
  5. @PathVariable注解
  6. 剑指offer系列26--正则表达式匹配
  7. 【BZOJ】【2938】【POI2000】病毒
  8. 使用go语言后的感受
  9. Python 计算程序运行时间
  10. javascript 延时执行函数
  11. PHP的几个常用加密函数【转载】
  12. Appium python自动化测试系列之页面滑动原理讲解(十)
  13. HTML5 拖放(Drag 和 Drop)详解与实例
  14. InnoDB的4个特性
  15. golang web实战之二(iris)
  16. [转]Windows下安装storm-0.9.1
  17. epoll+socket实现 socket并发 linux服务器
  18. Killing Monsters(hdu4970)
  19. Java 策略模式(Strategy)
  20. rhel7.x配置本地yum

热门文章

  1. 使用python抓取App数据
  2. ES6学习笔记(十二)异步解决方案Promise
  3. 二 MapReduce 各阶段流程分析
  4. iBatis框架使用 4步曲
  5. How to search Installed Updates
  6. php数组增加元素
  7. C#泛型链表Demo
  8. Linux安全应用之防垃圾邮件服务器的构建
  9. Ubuntu18.04上使用LLDB调试Chromium Android C++代码。
  10. Linux常用系统管理软件