- , Objects
Properties: Describes an object's information method: Describes the behavior of an object encapsulation: only care about input and output (regardless of how the process is implemented)
ü Categories of objects:
Built-in objects (native objects): Are predefined objects in the JavaScript language (such as String/number).
Host objects: Generally implemented by the browser environment, mainly divided into BOM and DOM.
Custom objects: Objects that are created autonomously by developers.
ü Creation of the object:
var Object Name = {
Property name : Property value ,
Method name : function () { method body } ---[equivalent to the right half of the literal definition function ]
}
- constructor function mode
( 1 ) var Object Name = new Object (); --- new-keyword, which means to create an object
Object Name . Property name = Property Value ; ---Object-is an ancestor of objects in JavaScript
Object Name . Method Name = function () {};
( 2 ) Custom Constructors
(1) An object must exist in advance Hero1
Eg:var Hero2 = object.create (hero1); The new object created by---has the same properties and methods as the object being passed
(1) Create an empty object
Eg: var object name = object.create (null); Hero4.name = ' zhaomin ';
U Use Object
Method One method two
Console.log ( object name . Property name ); console.log ( object name [' property name ']);
Object Name . Method Name (); object name [' method name '] () ;
[Note: Not applicable to complex naming methods] [Note: For complex naming methods] [More general usage]
Eg:content-type:number---writing errors
'content-type': number; Console.log (Hero.content-type); ---output error
The "-" is resolved to the subtraction operator; Hero.content-Property acquisition failed; Type-not a variable
Console.log (hero[' content-type '); ---correct
Attached: Window object
* is an object in the BOM that represents the window of the current browser; is also the top-level object in the browser environment (global object)
* Defined global variables and global functions, properties and methods added for Window objects
Eg:var name = ' Zhao min ';
var sayme = function () {
Console.log (' My name is Zhaomin ');
}
Console.log (Window.name);
Window.sayme ();
Properties and methods of the U-action object
2 new objects
Object Name . the new property name = Property Value ; the name of the object . the new method name = function () {};
2 modifying objects
Object Name . Original Property name = the new property value ; the name of the object . Original Method Name = function () {};
2 deleting objects
Delete Object Name . Property name Delete Object Name . Method Name
U traversal (enum) object
Only the object name is known, but the property and method name typeof or instanceof are unclear;
This method accesses all the enumerable properties of an object and its prototype chain in turn.
Format: for (var A in object name) {A---Represents the object's property name and method name
if (typeof hero[a] = = ' function ') {Distinguishing properties and methods-typeof operator
Hero[a] (); ---must be the method of the object
} else {
Console.log (Hero[a]); ---must be an object's property
}
Result: Gets all the properties or method names of the specified object
- Object.keys (obj) method:
The obj---represents the traversal of the current object return value: An array containing all the property names and method names of the current object eg:var a = Object.keys (Hero); Console.log (a); ["Name", "Age", "Sayme"]
- Object.getownpropertynames (Object) method:
This method is the same as above.
U detects the properties or methods of an object (that is, whether the specified attribute exists in the detection object:)
* Use the IN keyword console.log (' name ' in FN);
Use the hasOwnProperty () method Console.log (Fn.hasownproperty (' name ') for object objects);
Use undefined to judge console.log (fn.name = = = undefined);
Use the IF statement to determine. if (fn.name) {
Console.log (' name attribute exists ');
}
Note: The call object does not exist for the property---undefined
A method that calls an object that does not exist---output error is a function
- , functions, and objects
JavaScript Basics (objects, functions, and objects)