Js constructor creates Person class

Keywords: Javascript Erlang

1 /*
* Create a constructor dedicated to creating Person objects
* A constructor is a normal function, and it is created in the same way as a normal function.
* The difference is that constructors are customarily capitalized
*
* The difference between constructors and ordinary functions is the way they are called.
* Ordinary functions are called directly, while constructors need to be called using the new keyword.
*
* The execution flow of the constructor:
* 1. Create a new object immediately
* 2. Set the new object to this in the function, and you can use this in the constructor to refer to the new object
* 3. Execute the code in the function line by line
* 4. Return the new object as a return value
*
* Objects created using the same constructor are called a class of objects, and a constructor is also called a class.
* We will create an object through a constructor called an instance of this class.
*
* In this case:
* 1. When called as a function, this is window.
* 2. When called as a method, who calls method this is who
* 3. When called as a constructor, this is the newly created object
*
*/

<script type="text/javascript">
			function Person(name,age,gender){
				var name,age,gender;
				this.name=name;
				this.age=age;
				this.gender=gender;
			}
			
			function Animal(name,age,gender){
				var name,age,gender;
				this.name=name;
				this.age=age;
				this.gender=gender;
			}
			
			var P1=new Person("Liu",20,"male");
			var P2=new Animal("Siberian Husky",5,"male");
			console.log(P1.name);
			console.log(P2.name);
			/*
			 * Using instanceof, you can check whether an object is an instance of a class.
			 * 	Syntax:
			 * 		Object instanceof constructor
			 * If so, return true or false
			 */

			console.log( P2 instanceof Person);
		</script>

enumeration

          function Person(name,age,gender){
				var name,age,gender;
				this.name=name;
				this.age=age;
				this.gender=gender;
			}
			var P1=new Person("Liu",20,"male");
			
			for(var n in P1){
				console.log(n);
				console.log(P1[n]);
			}

Result:

1. examples

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
				function Person(name , age , gender){
				this.name = name;
				this.age = age;
			}
			
			//To String for Modifying Person Prototype
			Person.prototype.toString = function(){
				return "Person[name="+this.name+",age="+this.age+"]";
			};
			
			//Create a Person object
			var per = new Person("Sun WuKong",18);
			var per2 = new Person("Zhu Bajie",28);
			var per3 = new Person("Red child",8);
			var per4 = new Person("spider goblin",16);
			var per5 = new Person("Erlang God",38);
			
			/*
			 * Put these person objects into an array
			 */
			var perArr = [per,per2,per3,per4,per5];
			
			/*
			 * Create a function that extracts Person s over 18 years old from perArr.
			 * 	It is then encapsulated in a new array and returned
			 * arr
			 * 	Formal parameters, an array of information to be extracted
			 */
			function getAdult(arr){
				//Create a new array
				var newArr = [];
				
				//Traverse arr to get the Person object in arr
				for(var i=0 ; i<arr.length ; i++){
					var p = arr[i];
					//Determine whether the age of the Person object is greater than or equal to 18
					if(p.age >= 18){
						//If greater than or equal to 18, add this object to newArr
						//Put objects in a new array
						newArr.push(p);
					}
				}
				//Return a new array
				return newArr;
				
			}
			
			var result = getAdult(perArr); 
			
			console.log(result);

		</script>
	</head>
	<body>
	</body>
</html>

2. examples

<script type="text/javascript">
			
			/*
			 * Usually we use for loops to traverse arrays.
			 * 	JS It also provides us with a way to traverse arrays.
			 * forEach()
			 * 		- This method only supports browsers over IE8
			 * 			IE8 This method is not supported by browsers below, so if IE8 compatibility is required, do not use forEach.
			 * 			Or use the for loop to traverse
			 */
			
			//Create an array
			var arr = ["Sun WuKong","Zhu Bajie","Sand monk","Tang Seng","White bone essence"];
			  
			/*
			 * forEach()Method requires a function as a parameter
			 * 	- Functions like this, created by us but not called by us, are called callback functions.
			 * 	- Several element functions in an array are executed several times, and each time they are executed, the browser traverses the elements.
			 * 		Passing in as arguments, we can define parameters to read them.
			 * 	- The browser passes three parameters in the callback function:
			 * 		The first parameter is the element currently traversing
			 * 		The second parameter is the index of the element currently traversing
			 * 		The third parameter is the array being traversed.
			 * 		
			 */
			arr.forEach(function(value , index , obj){
				console.log(index+":"+value+obj);
			});
			
			
		</script>

3. examples

<script type="text/javascript">
			
			var arr = ["Sun WuKong","Zhu Bajie","Sand monk","Tang Seng","White bone essence"];
			
			/*
			 * slice()  [)
			 * 	- Can be used to extract specified elements from an array
			 * 	- This method does not change the element array, but encapsulates the intercepted elements into a new array and returns them.
			 * 	- Parameters:
			 * 		1.Intercept the index of the starting position, including the starting index[
			 * 		2.Index of the truncated end position, excluding the end index)
			 * 			- The second parameter can be omitted without writing, which intercepts all elements from the beginning of the index to the end.
			 * 		- An index can pass a negative value, and if it passes a negative value, it is calculated from the back to the front.
			 * 			-1 The penultimate
			 * 			-2 Second to last
			 */
			
			var result = arr.slice(1,4);
			
			result = arr.slice(2);
			
			result = arr.slice(1,-2);//Pig eight quit, "sand monk"
			
			console.log("Look here:"+result);
			
			/*
			 * splice()
			 * 	- Can be used to delete specified elements in an array
			 * 	- Using splice() affects the original array, removing the specified elements from the original array
			 * 		Returns the deleted element as a return value
			 * 	- Parameters:
			 * 		First, an index representing the starting position
			 * 		Second, it represents the number of deletions
			 * 		The third and later...
			 * 			You can pass in some new elements that will be automatically inserted in front of the start location index
			 * 	
			 */
			
			arr = ["0 Sun WuKong","1 Zhu Bajie","2 Sand monk","3 Tang Seng","4 White bone essence"];
			//var result = arr.splice(1,2,'Bull Devil King','Iron Fan Princess','Red Boy'); // delete two elements from the position of subscript 1 and add three "Bull Devil King",'Iron Fan Princess','Red Boy'elements to the position of subscript 1.
			//var result = arr.splice(1,2); the effect is to delete two elements from the place marked 1
			
			console.log(arr);
			//console.log(result); 
			
			
		</script>

4. examples

<script type="text/javascript">
			
			//Create an array
			var arr = [1,2,3,2,2,1,3,4,2,5];		
			
			//Remove duplicate numbers from an array
			//Get each element in the array
			for(var i=0 ; i<arr.length ; i++){
				//console.log(arr[i]);
				/*Get all the elements after the current element*/
				for(var j=i+1 ; j<arr.length ; j++){
					//console.log("---->"+arr[j]);
					//Determine whether the values of two elements are equal
					if(arr[i] == arr[j]){
						//If equality proves that duplicate elements exist, then delete the elements corresponding to j
						arr.splice(j,1);
						//When the element in which j is currently located is deleted, the elements behind it are automatically complemented.
						//Now, instead of comparing this element, I need to compare the elements where j is located once.
						//Make j self decreasing
						j--;
					}
				}
			}
			
			console.log(arr);
			
			
		</script>

Posted by cptn_future on Wed, 18 Sep 2019 03:04:40 -0700