js对象基础写法练习

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

  

转载于:https://www.cnblogs.com/zhujiasheng/p/6133920.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值