Basic Introduction to javascript object-oriented

Source: Internet
Author: User

What is an object?

To put it simply, the objects in programming languages are simplified for things in reality. For example, we are an object, but it is difficult for programming languages to fully describe such a complex object. Therefore, we must simplify the process. First, we should simplify people into a combination of attributes and actions, and then only keep the attributes and actions that are meaningful to the program. For example, if we develop a program to count the heights of people in a school, we can omit the human behaviors in this program and only keep the behaviors, only the height attribute is retained. In this way, we get the simplest object.

JavaScript string object

Object Attributes
In fact, we are already using objects in html dom. For example, we know that DOM nodes have some information, such as nodeName and nodeType, which are actually the attributes of node objects. The following uses a string as an example to check the attributes of an object.

String attributes
Var s = "I have a 7 character"; after defining the string s, we have a string object. We can access its length attribute (length ), this attribute does not need to be defined. It is a built-in attribute. The access method is as follows:

S. length click the button below to see the length of the string.

Alert (s. length)
String object method (behavior)
Similarly, an object can have behavior. Taking a String object as an example, we can let the string return letters or texts at a certain position. This is an action. You can use the buttons below to experience the attributes and methods of the string. This article will explain in detail how to use each method.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var s = "I have 7 characters"; var str = "character" + "string"; // Add two strings
Alert (str)
</Script>

Use the length attribute of the string to set the length of the string.

Alert (str. length)
Use the charAt method to return the characters at the specified position.

Alert (str. charAt (0 ))
Alert (str. charAt (1 ))

The substring method truncates a substring from a string.

Alert (str. substring (0, 2 ))
IndexOf returns the position of the specified character or string in the string. If the character does not exist,-1 is returned.

Alert (str. indexOf ('delimiter ')
The lastIndexOf method returns the position of the last occurrence of a character in a string.

Date object

Instance JavaScript date Code
Let's take a look at the JS Code that uses the date object. Click the buttons below to view the values of each variable.

Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var today = new Date (); // create a Date object
Var todayStr = today. toString (); // converts a date to a string.
Var todayLocal = today. toLocaleString (); // convert to a local string
Var date = today. getDate (); // query the current month date.
Var day = today. getDay (); // query the current day of the week
Var month = today. getMonth (); // query the month
Var year = today. getFullYear (); // query the year
Alert (todayStr );
Alert (todayLocal );
Alert (date );
Alert (day );
Alert (month );
Alert (year );
</Script>

Create a Date object
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.

String object method (behavior)
JavaScript date object query (get) Method
JavaScript date object setting (set) Method
JavaScript date object conversion (to) Method
JavaScript Date object application instance-Clock Code
This Code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.