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

May 16, 2016 pm 05:50 PM
object-oriented

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.
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement object-oriented event-driven programming using Go language How to implement object-oriented event-driven programming using Go language Jul 20, 2023 pm 10:36 PM

How to use Go language to implement object-oriented event-driven programming Introduction: The object-oriented programming paradigm is widely used in software development, and event-driven programming is a common programming model that realizes the program flow through the triggering and processing of events. control. This article will introduce how to implement object-oriented event-driven programming using Go language and provide code examples. 1. The concept of event-driven programming Event-driven programming is a programming model based on events and messages, which transfers the flow control of the program to the triggering and processing of events. in event driven

What is the importance of @JsonIdentityInfo annotation using Jackson in Java? What is the importance of @JsonIdentityInfo annotation using Jackson in Java? Sep 23, 2023 am 09:37 AM

The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate object identity during serialization and deserialization. ObjectIdGenerators.PropertyGenerator is an abstract placeholder class used to represent situations where the object identifier to be used comes from a POJO property. Syntax@Target(value={ANNOTATION_TYPE,TYPE,FIELD,METHOD,PARAMETER})@Retention(value=RUNTIME)public

Explore object-oriented programming in Go Explore object-oriented programming in Go Apr 04, 2024 am 10:39 AM

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

PHP Advanced Features: Best Practices in Object-Oriented Programming PHP Advanced Features: Best Practices in Object-Oriented Programming Jun 05, 2024 pm 09:39 PM

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

Are there any class-like object-oriented features in Golang? Are there any class-like object-oriented features in Golang? Mar 19, 2024 pm 02:51 PM

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

Analyzing the Flyweight Pattern in PHP Object-Oriented Programming Analyzing the Flyweight Pattern in PHP Object-Oriented Programming Aug 14, 2023 pm 05:25 PM

Analyzing the Flyweight Pattern in PHP Object-Oriented Programming In object-oriented programming, design pattern is a commonly used software design method, which can improve the readability, maintainability and scalability of the code. Flyweight pattern is one of the design patterns that reduces memory overhead by sharing objects. This article will explore how to use flyweight mode in PHP to improve program performance. What is flyweight mode? Flyweight pattern is a structural design pattern whose purpose is to share the same object between different objects.

Analysis of object-oriented features of Go language Analysis of object-oriented features of Go language Apr 04, 2024 am 11:18 AM

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.

In-depth understanding of PHP object-oriented programming: Debugging techniques for object-oriented programming In-depth understanding of PHP object-oriented programming: Debugging techniques for object-oriented programming Jun 05, 2024 pm 08:50 PM

By mastering tracking object status, setting breakpoints, tracking exceptions and utilizing the xdebug extension, you can effectively debug PHP object-oriented programming code. 1. Track object status: Use var_dump() and print_r() to view object attributes and method values. 2. Set a breakpoint: Set a breakpoint in the development environment, and the debugger will pause when execution reaches the breakpoint, making it easier to check the object status. 3. Trace exceptions: Use try-catch blocks and getTraceAsString() to get the stack trace and message when the exception occurs. 4. Use the debugger: The xdebug_var_dump() function can inspect the contents of variables during code execution.

See all articles