JS objects Introduction

<! DOCTYPE HTML> 
<HTML> 
	<head> 
		<Meta charset = "UTF-. 8"> 
		<title> </ title> 
		<Script type = "text / JavaScript"> 
			/ * 
			 * Data Type JS 
			 * - String String 
			 * - Number value 
			 * - Boolean Boolean value 
			 * - null null 
			 * - undefined undefined 
			 * - more than five types belong to the basic data types, the value of the future we see 
			 * if not the top five species, all objects 
			 * - Object Object 
			 * 
			 * 
			 * basic data types are single value "Hello" to true 123, 
			 * there is no link between the value and the value. 
			 ** / 
	1, Introduction to Object 
			/ * object belongs to a composite data type, the object can be stored in a plurality of different types of data attributes. 
			 * 
			 * Category object: 
			 * 1.
			 * - for example: the Math String Number The Boolean Function Object .... 
			 * 
			 * 2. host objects 
			 * - objects provided by operating environment JS, the current situation mainly refers to the object provided by the browser 
			 * - such as BOM (Browser) console 
			 the DOM the Document * 
			 * 
			 * 3. custom object 
			 * - an object created by the developers themselves 
			 * 
			 * / 
			
	2, create the object 
			mode 1: 
			/ * 
			 * use the new keyword function call, the constructor constructor 
			 * constructor is devoted function to create the object 
			 * need not define a class by class, directly create objects, attributes may be dynamically added and removed 
			 * the typeof a check object returns object 
			 * / 
			var obj = new new object (); 
			/ * 
			 * in a subject stored attribute value is called 
			 * add attributes to the object 
			 * syntax: Object attribute name = attribute value;. 
			 * / 
			mode 2: 
					/ * 
                 * Using object literal to create an object 
                 * / 
					var obj = {}; 
					//console.log(typeof obj); // Object 
					obj.name = "Monkey"; 
					//console.log(obj.name) ; 
		
					/ * 
					 * using object literal may create the object, object attribute specified directly 
					 * syntax: {property name: property value, the property name: property value ....} 
					 * object literal property name can be added quotes can not add, it is recommended not to increase 
					 * If you want to use some special name, it must be quoted 
					 * 
					 * attribute names and values are a set of name-value of a group structure, 
					 between the * name and value to use: connect, and more used between a name-value pairs, spaced 
					 * if a property after no other properties, and do not write comma 
					 * / 
					var obj2 = { 
						name: "pig", 
						Age: 13 is, 
						Gender: "M", 
						Test: { name: "Drifting"} 
					};
					the console.log (obj2.test); 
			

	. 3, add the attribute 
			mode 1: 
					// add a name attribute in the obj 
					obj.name = "Monkey"; 
					// add a gender attribute to the obj 
					obj.gender = "M"; 
					// add a property to the age obj in 
					obj.age = 18; 

					/ * 
					 * reads object attribute 
					 * syntax: Object properties 
					 * If the object does not attribute to read, does not complain but will return undefined 
					 * / 

					//console.log(obj.gender); 
					//console.log(obj.hello); 
			embodiment 2: 
			/ * 
			 . * to use a particular property name can not be used to operate the way 
			 * need to use another way : 
			 * syntax: Object [ "attribute name"] = attribute value 
			 * need to read this way 
			 * 
			 * using [] attribute to operate this form, more flexible, 
			 * a variable can be passed directly in [] so that the variable value is how much the property will be read 
			 *
			 * / 
			Obj [ "123"] = 789; 
			obj [ "NiHao"] = "hello"; 
			var n-= "NiHao"; 
			//console.log(obj["123 "]); 

. 4, delete, and modify attributes 
			/ * 
			 * modify an object's property values 
			 . * syntax: name = new object property value 
			 * / 
			obj.name = "tom"; 
			/ * 
			 * deleted object's properties 
			 * syntax:. delete object property name 
			 * / 
			the delete obj.name; 

5, the attribute value is another object 
			/ * 
         * attribute value 
         * JS object attribute value may be any data type 
         * or may be an object 
         * / 
			obj.test = to true; 
			obj.test = null; 
			obj.test undefined =; 

			// Create an object 
			var obj2 = new new Object (); 
			obj2.name = "pig";

			// set the attributes of obj obj2
			= obj2 obj.test; 
			the console.log (obj.test.name); // Pig 

6, in the operator 
/ * 
                         * in operator 
                         * - can be checked by the operator whether the object contains a specified attribute 
                         * if returns true, did not return false 
                         * - syntax: 
                         * "attribute name" in the object 
                         * / 
			//console.log(obj.test2); 

			// check whether it contains obj test2 property 
			//console.log("test5 " obj in); // false 
			//console.log("test "in obj); // to true 
			console.log (" name "in obj);to true // 

7, the difference between the basic data types and reference data types 
			/ * 
			 * basic data types 
			 * Number The Boolean String Null Undefined 
			 * 
			 * reference data types 
			 * Object 
			 * 
			 * the JS variables are saved to the stack memory, 
			 * the value of the basic data types is stored directly in the memory stack, 
			 * value between the value of an independent existence, modify a variable does not affect the other variables 
			 * 
			 * objects are saved to the stack memory, each create a new object, it will open up a new space in the heap, 
			 * and is stored in the variable memory address of the object (reference object), if two variable is stored with an object reference, 
			 * when a variable by modifying a property, the other will be affected 
			 * / 

			var obj = new new Object (); 
			obj.name = "Monkey"; 
			var obj2 = obj; 
			// modify the name attribute of obj 
			obj.name = "pig"; 
			/*console.log(obj.name); pig 
			console.log (obj2.name); * / pig 

			// set obj2 is null, does not affect the obj 
			obj2 = null ; 
			/*console.log(obj); 
			the console.log (obj2); * / null 

			var C = 10;
			D 10 = var; 
			//console.log(c == D);

			OBJ3 new new Object = var (); 
			var OBJ4 = new new Object (); 
			obj3.name = "sand monk"; 
			obj4.name = "sand monk"; 

			/*console.log(obj3); // name obj = { " Shahe Shang} 
			the console.log (OBJ4); // obj {name = "sand monk} 
			/ * 
			 * when comparing the value of two basic types of data, is a comparison value. 
			 * When comparing the two and reference data types, it is more the memory address of the object, 
			 * if two objects are exactly the same, but different addresses, it also returns false 
			 * / 
			console.log (obj3 == OBJ4) ; // to false 
		</ Script> 
	</ head> 
	<body> 
	</ body> 
</ HTML>

 

Guess you like

Origin www.cnblogs.com/luoxuw/p/11440252.html
Recommended