JavaScript (4)-object literal volume, this, call () and apply () methods, JS Exception Handling

Source: Internet
Author: User

Today, we will focus on the object-oriented content of javascript: object literal, this, this, call () and apply () methods, and JS exception handling.

Object literal:

1. The examples we have seen earlier use dot identifiers to create objects and members. For example:

var temp=document.getElementById('example');

We used a vertex to obtain the getelementbyid Member of the Document Object, And then we executed the Member. Of course there is also a parameter, and then we assigned the object to the temp variable.

2. Of course, we have also used the dot operator to define methods in prototype:

myConstructor.prototype.clearMessage=function(){}

Prototype is equivalent to an object in a constructor. First, we create a constructor. This myconstructor has a prototype object by default. They add a member clearmessage to this object, this member is a function. We assign this anonymous function to the clearmessage member variable.

This is equivalent to defining a common method clearmessage in myconstructor. Let's look at an example:

<script type="text/javascript">function myConstructor(){//}myConstructor.prototyepe.clearMessage=function(){alert("");}var obj=new myConstructor();obj.clearMessage();</script>

This is what we want to talk about.

3. One object literal is used as another syntax, Which is clearer and easier to read:

4. the "Key/value" in the {key: value, key: Value} syntax will become a static member of the object. If the value specified for a "jian" is an anonymous function, the function becomes a static method of the object; otherwise, it is a static attribute of the object. This syntax structure is similar to the JSON syntax. You only need to remember that the object literal syntax will automatically create an object instance, that is, you cannot use the new keyword to instantiate it again.

5. If you want to use the same object literal syntax to create a constructor with a public method, you still need to start from the function object used as the constructor:

The difference is whether the call is performed before or after the definition.

Note: If you use the object literal, you must close the end Of the comma. If you put a comma at the end of the last item when defining an object, the value of the last item will become null.

This:

This is something that is hard to comprehend;

1. A keyword that depends on the execution environment using it and is parsed.

<SCRIPT type = "text/JavaScript"> var sound = "admin"; function myfunction () {This. style. color = "red"; alert (sound );} </SCRIPT> 

This code is used to obtain the tag and then call this myfunciton. Then, this is an instance of the myfunciton object, which is the <a href = "#" id = "A"> hyperlink used for testing </a>.

So why should we put this sentence: Document. getelementbyid ("A'). onclick = myfunction; in the body? Because the code is executed from the top down, if you put this sentence before the tag, then an error is reported because the tag has not been loaded yet. Or you can write it in the window:

<SCRIPT type = "text/JavaScript"> var sound = "admin"; function myfunction () {This. style. color = "red"; alert (sound);} window. onload = function () {document. getelementbyid (""). onclick = myfunction ;} </SCRIPT> 

Or you can add onload to the body.

If we write this:

<script type="text/javascript">var sound="admin";function myFunction(){this.style.color="red";alert(sound);}myFunction();</script>

So an error is reported. What is the error?

Because our myfunction is defined on the window, which is equivalent:

<script type="text/javascript">var sound="admin";function myFunction(){this.style.color="red";alert(sound);}window.myFunction();</script>

This. style. color = "red"; at this time, this points to the window. This is equivalent to modifying the style of the window. It will certainly report an error.

Call () and apply () Methods:

Forcibly reference this reference to an object.

JS Exception Handling:

JS uses try {} and catch () for representation.

<SCRIPT type = "text/JavaScript"> function myfunction () {This. style. color = 'green';} Try {myfunction ();} catch (exception) {alert ('caught Exception name: '+ exception. name + '\ n:' + exception. message) ;}</SCRIPT>

The result is as follows:

This is much better than the naked report, and exception handling is also possible.

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.