What is an object
To put it simply, objects in programming languages are simplifications of things in reality. For example, a person is an object, but it is difficult for a programming language to fully describe such a complex object. So we must make simplifications. First, simplify people into a combination of attributes and behaviors, and then retain only a few attributes and behaviors that are meaningful to the program. For example, if we make a program that counts the heights of people in a certain school, then we can omit people's behaviors in this program, retain only behaviors, and only retain the attribute height. In this way, we get the simplest object.
JavaScript String Object Properties of Object
In fact, we have already used objects in HTML DOM before. For example, we know that DOM nodes have some information, such as nodeName, nodeType, etc. In fact, this information is the properties of the node object. Let's take a string as an example to look at the properties of the object.
String attributes
var s = "I have 7 characters"; After defining the string s, we have a string object, and we can access its length attribute ( length), this property does not need to be defined by us, it is a built-in property. The access method is as follows:
s.length Click the button below to see the length of the string.
alert(s.length)
Methods (behaviors) of string objects
Similarly, objects can also have behaviors. Taking the string object as an example, we can let the string return to a certain position letters or words, this is an action. You can use the buttons at the back to simply experience the various properties and methods of strings. At the end of this article, the use of each method will be explained in detail.
Use the length property of the string to be the length of the string.
alert(str.length)
Use the charAt method to return the character at the specified position.
alert(str.charAt(0))
alert(str.charAt(1))
substring method intercepts a substring from a string.
alert(str.substring(0,2))
indexOf returns the position of the specified character or string in the string, or -1 if the character does not exist.
alert(str.indexOf('symbol')
lastIndexOf method returns the position of the last occurrence of a character in a string.
date object
Example JavaScript date code
Let’s first look at a piece of JS code that uses the date object. Click the buttons below to see the values of each variable.
Create a new Date object We use the following statement to Create a Date object.
var today = new Date(); After execution, the created Date object is saved in the today variable. Method (behavior) of the string object
JavaScript date object query (get) method
JavaScript date object setting (set) method
JavaScript date object conversion (to) method
JavaScript Date object application example - clock code
This code is transferred from w3schools. com.
Array object Create a JavaScript array
Example JavaScript array code
The following is a simple Using the JS code of the array, you can click the button behind to observe the value of each variable.
The function defined using this method is exactly the same as the function above, but this syntax is more troublesome and is generally not used.
(Function) call method of Function object
call is a very useful method. It can control the running environment of the function, that is, control the object pointed to by this inside the function. The following example can illustrate this problem:
function whatsThis(){ alert(this); }When we call the above function, we will see that this points to the window. Try it out:
whatsThis()
But if call is used, we can control the pointing of this inside the function, for example:
whatsThis.call(document)()
The above code uses the call method of the function object to point this inside the function to the document.
If the original function needs to accept parameters, such as the add function, you can use the following form:
add.call(document,1,2) In other words, the first parameter of call is bound to this Object, and 1 and 2 are the parameters that the original add function needs to accept.
(Function) Apply method of Function object
The method of using apply is basically the same as that of call, except that the parameters are passed in the form of an array, or the add function is used as an example:
add.call(document,[ 1,2]) It can be seen that the two numeric parameters that the original function add needs to accept are passed into apply in the form of an array.
(Function) caller attribute of Function object
function whoCalls(){ alert(whoCalls.caller); } function SheCalls(){ whoCalls(); }whoCalls()SheCalls()
Use the caller attribute, You can find out who called the current function. Note that the caller is only valid inside the function body.
(Function) The arguments attribute of the Function object
Javascript functions can accept any number of parameters, so when defined, the number of parameters does not limit the function's ability. In a function, we can use arguments to access the parameters passed into the function, for example:
function howmany(){ var num = arguments.length; alert(num); } The howmany function will output the number of parameters passed to the function. , click on the buttons below to try it out.
howmany(1,2,3,4)howmany(1,2,3,4,5,6,7,8)
Function arguments.callee
We already know that function will have arguments attribute, Arguments.callee is the function currently being executed, for example:
function whoAmI(){ alert(arguments.callee); }whoAmI()
Executing the above function will display the source code of the current function. Of course, we can call callee again, which is mainly used for anonymous function recursion.