JavaScript Object 1. What is a JavaScript object?
All things in JavaScript are objects: strings, numbers, arrays, dates, and so on.
In JavaScript, objects are data that owns properties and methods.
Classification of 2.JavaScript Objects (1) built-in objects
The internal objects in JS include array, Boolean, Date, Function, Global, Math, Number, object, REGEXP, string, and various error class objects, including errors, Evalerror, Rangeerror, Referenceerror, SyntaxError and TypeError.
The two objects, global and math, are also known as "built-in objects," which are created when the script is initialized and do not have to instantiate both objects.
(2) Custom objects
- By means of the Var obj={} object Literal (literal) method
- Create object from constructor: function person () {} var person=function () {}
- Creating objects from Object.create
3. Accessing the properties and methods of an object
(1) Attribute operation
- Accessing properties
- Add Property
- modifying properties
- Delete Property
- Traverse Properties
(2) Access method: Object name. Method Name () Eg:obj. Name Object name [method name] eg:obj["name"]
Note: 1. When referencing an existing object property and having a property name, the point is the same as the square bracket, and if the property is an array or an object with no attribute name, use the brackets + subscript in the same way as the array element reference.
2. When creating a function attribute, if the property name is known. The same way you use the dot and the brackets. If the property name is a set of variables, it can be expressed in brackets only . The flexible application of brackets can make the code very powerful.
4. Example use
1. Guess the number of games, there is a 0-100 random number of players 5 times the opportunity to guess, each time to guess, the results will tell the player is small, or big, if in 5 chance to guess, the output of "smart", the program ends, if 5 times to complete the program directly end.
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0"> <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge"> <title>Document</title></Head><Body> <!--1. Guess the number of games, there is a 0-100 random number of players have 5 times to enter the opportunity to guess,//every time to guess, will tell the result of the player is small, or big//if in 5 chance guessed, output "smart", program end//if 5 times machine will run out of program directly. - <Script> vararr=parseint (Math.random ()* -); for(varI=5; I>0; I--) {n=parseint (Window.prompt ("Please enter a number of 0-99"+"only"+I+"Sub-opportunity amount")); if(n>arr) {Alert ("this number is larger than the random number, and"+(i-1)+"Sub-opportunity amount"); } Else if(n<arr) {Alert ("This number is smaller than the random number, and"+(i-1)+"Sub-opportunity amount"); } Else if(n==arr) {Alert ("Smart, guess the right amount."); }} alert ("number is"+arr+"fancy it."); </Script></Body></HTML>
2. Enter 2 dates (month and year) to calculate how many days are different for two dates?
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <Metaname= "Viewport"content= "Width=device-width, initial-scale=1.0"> <Metahttp-equiv= "X-ua-compatible"content= "Ie=edge"> <title>Document</title></Head><Body> <Script> /*3. Enter 2 dates (month and year) to calculate how many days are different for two dates? */ vararr=NewArray (); arr[0]=NewDate (Window.prompt ("Please enter a first date")); arr[1]=NewDate (Window.prompt ("Please enter a second date")); var Time=0; time=(arr[0]-arr[1])/ +/ -/ -/ -;//time= (arr[0]-arr[1])/(24*3600*1000) calculates the number of daysdocument.write (Math.Abs (time)); </Script></Body></HTML>
This is the first time a small sister to write a blog, for JavaScript objects are also in the study of learning, write the shortcomings of the place also please many forgive, I hope to communicate with you more learning. Thank you for your busy schedule to read my essay, if you want to learn more javascript knowledge, you can pay attention to the Zhiliaotang.com study community amount.
"Cicada Hall Study Notes"/javascript object--/warm Nicole