JavaScript Object learning summary and javascript Object Summary

Source: Internet
Author: User

JavaScript Object learning summary and javascript Object Summary

JavaScript Object

JavaScript provides multiple built-in objects, such as String, Date, and Array.

Objects are only special data types with properties and methods.

In JavaScript, almost every js is inseparable from its objects. Next we will take a closer look at js objects.

The classification of objects in js is the same as the classification of functions in the language we have learned before. One is the predefined object that comes with the system, and the other is the custom object written by the user.

Shows the general content:

I. Core objects

1. Array object

Array object, used to create an array, use new to create

Common methods include:

Common attributes:

The example shows how to create and use an Array object.

<Script language = "javascript"> var data = new Array ("1", "2", "3"); // create an Array data document. writeln ("the initial content of data is:" + data + ","); // output array Content function fun () {// custom function fun () show the length of the array if (this. length! = 0) {document. writeln ("<PRE> the length of this array is:" + this. length + "</PRE>");} else {document. writeln ("<PRE> the length of this Array is 0 </PRE>") ;}} Array. prototype. fun = fun; // Add the fun () method data to the Array object. fun (); // call the fun () method data = new Array (); // clear the data Array content data. fun (); // display the length of the cleared array </script>

Running result:

Note:

Prototype is used to add new attributes and Methods. Its usage is Array. prototype. functionName = functionName.

2. Function object

Function objects can dynamically generate and call functions during program execution.

Common attributes are as follows:

The common method is as follows:

The following is an example:

<Script language = "javascript">
FunSum = new Function ("x", "y", "z = x + y; return z"); // CREATE Function funSum ()

X = prompt ("Enter the X value :","");
Y = prompt ("Enter the Y value :","");

X = Number (x );
Y = Number (y );
Alert (newfun (x, y); // call the function and Output
</Script>


Note: Function objects cannot be written as smaller functions. A Function is an object that is used to dynamically create a function. A function is a keyword. A Function derived from Function new is also an instance of a function object.

3. String object

The String object provides attributes and methods for processing strings. You can assign values directly or create values using new.

There are many methods for String objects. They are mainly used for the display format, font size, font color, character search, and case-sensitive conversion of strings on the Web page.

Through the prototype attribute of the above object, we also see the characteristics of JavaScript, which not only provides us with some features, but also provides users with their own method of adding features, which is very good.

Ii. Document objects

Document Object. The first example of accessing JavaScript is to get the control name through the ID name, which uses document. If you didn't think much about it at the time, it should be a keyword.

The document object is the property of the window object and is also the only object that belongs to BOM and DOM.

Speaking of window objects, Let's first look at the relationship between objects in window. For example:

After learning about the general structure, let's take a look at the common attributes of the document Object:

For the document Object method, the most frequently used. write ()/. writeln () method is described here. The two methods are used to print characters. The difference is that there is a line break after writeln is printed.

Below is an example of dynamically adding JavaScript files using the write method of the document Object.

<script language="javascript">   document.write("<script type=\"text/javascript\" src=\"textuser.js\">"+"</scr"+"ipt>"); </script> 

Here we mainly want to explain why </script> is separated.

Let's take a look at the source code of the separately written webpage:

There is no difference. Next let's take a look at the source code of the webpage when it is not separated:

 

The result is obvious. If you do not open the </script> file, it will be paired with the previous <script> file. In this way, the browser will prompt us an error.

Iii. event object

An event object is an object only related to the event processing process.

Events in JavaScript are the same as those in vb, including mouse events, Keyboard Events, loading events, and so on ..

The event must have its departure. After the event is triggered, the event will end immediately.

It is easy to use events in JavaScript, that is, add On before the keyword.

The javascript Object learning summary shared in this article. For more information, please stay tuned to this site.

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.