Home > Web Front-end > JS Tutorial > Detailed introduction to the basics of getting started with javascript object-oriented_js object-oriented

Detailed introduction to the basics of getting started with javascript object-oriented_js object-oriented

WBOY
Release: 2016-05-16 17:50:16
Original
949 people have browsed it
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.

Copy code The code is as follows:



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
Copy code The code is as follows:




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.
Copy code The code is as follows:



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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template