JS Object Basic Practice

<!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:"+name);  
	     }  
	 }  
     
           // вот метод вызова
	 function student(name,age){  
	     Person.call(this,name,age);  
	 }  

	  // вот метод применения
	 function teach(name,love){
	 	// Person.apply(this,[name,love]);
	 	Person.apply(this,arguments);
	 }

	  // сходства и различия между Call и Aplly:  
         // 1, первый параметр, это то же самое, относится к текущему объекту  
         // 2, второй параметр отличается: вызов - это список параметров; применить массив (аргументы также могут быть))) 
     
     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 («Используйте прототип, чтобы получить имя:«+this.name);  
      }  

   function Student(){}  
	     Student.prototype = новый человек ("Хонг Рутонг", 21);  
	    var stu=new Student();  
	    Student.prototype.grade=5;  
	    Student.prototype.intr=function(){  
	        console.log(this.grade);  
    }  
       stu.sayhello (); // output: используйте прототип, чтобы получить имя: Hong Rutong  
             stu.intr (); // Выход: 5  
</script>

  

Leave a Comment