6. JS Objects and Functions

Object (Object)
- Object is a reference
- Object is a composite data type that can save multiple properties of different data types in
an object - When using typeof to check an object, it will return object - Create an object- Method One: - var obj = new Object(); - Method two: - var obj = {}; - Add property to object- Syntax: object. property name = property value; object["property name"] = property value; - There is no requirement for the attribute name of the object, and it does not need to comply with the specification of the identifier, but in the development, try to write according to the requirements of the identifier. - Attribute values ​​can also be of any data type. - read properties in an object - syntax: object.property name object - if you read a property that does not exist in an object, it will not report an error, but return an undefined - delete a property in an object - syntax : delete object. property name delete object ["property name"] - use in to check whether the object contains the specified property - syntax: "property name" in object





























- Returns true if the property is present in the object and returns false
if not - Use object literals to add properties directly to the object when creating the object Syntax : var obj = { property name:property value, property name:property value , property name:property value, property name:property value } - basic data type and reference data type - basic data type String Number Boolean Null Undefined - reference data type Object - data of basic data type, the variable directly saves its value . Variables are independent of each other, and modifying one variable will not affect other variables. - Data of a reference data type, a variable is a reference (memory address) of the stored object. If multiple variables point to the same object, modifying the properties of one variable will affect other variables. - When comparing two variables, for the basic data type, the comparison is the value, and for the reference data type, the comparison is the address, and the address is the same . Encapsulates some code that can be executed by calling a function when needed - checking a function with typeof returns function - creating a function - function declaration




























function function name([parameter1,parameter2...parameterN]){
statement...
} - function expression var functionname = function([parameter1,parameter2...parameterN ]){ statement... }; - call function - syntax: function object ([argument 1, argument 2...argument N]); fun() sum() alert() Number() parseInt() - When we call a function, the code encapsulated in the function will be executed in the order in which it was written - Formal parameters and actual parameters - Formal parameters: Formal parameters - When defining a function, you can define one or more formal parameters in (), formal parameters Defining a formal parameter is equivalent to declaring the corresponding variable in the function but not assigning it, and the formal parameter will only be assigned when it is called. - Actual parameters: actual parameters - When calling a function, you can pass the actual parameters in (), and the passed actual parameters will be assigned to the corresponding formal parameters. When calling the function, the JS parser will not check the type and number of the actual parameters, you can pass the actual parameters. A value of any data type. If the number of actual parameters is greater than the formal parameters, the redundant actual parameters will not be assigned a value, if the number of actual parameters is less than the formal parameters, the formal parameters without corresponding actual parameters will be assigned undefined












































Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324655474&siteId=291194637