JavaScript Object-oriented support (top)

Source: Internet
Author: User

Few people have systematically analyzed the object-oriented nature of JavaScript. I hope that the following text will give you an idea of the least known aspect of the language.

1. Types in JavaScript

--------

Although JavaScript is an object-based language, objects (object) are not the first type in JavaScript. JS is a function of the first type of language. In this way, not only because the functions in JS have a variety of features of the functions of the high-level language, but also because in JS, object is also implemented by the function. In this context, you can see a further explanation in the "Structure and destructor" section of the following article. JS is a weak type, his built-in type is simple and clear:

Undefined: Not defined

Number: Numbers

Boolean: Boolean value

String: Strings

Function: Functions

Object: 1). Undefined type

In IE5 and the following versions, any other action on undefined will cause an exception, except for direct assignment and typeof (). If you need to know whether a variable is undefined, you can only use the typeof () method:

<script>
var v;
if (typeof(v) == 'undefined') {
// ...
}
</script>

However, in IE5.5 and above versions, undefined is an implemented system reserved word. Therefore, undefined can be used to compare and compute. A simpler way to detect whether a value is undefined is to:

<script>
var v;
if (v === undefined) {
// ...
}
</script>

So in order for the core code to be (partially) compatible with IE5 and earlier versions, a line of code in the Romo core unit is used to "declare" a undefined value:

//---------------------------------------------------------
// code from Qomolangma, in JSEnhance.js
//---------------------------------------------------------
var undefined = void null;

The one line of code that needs to be explained is the application of the void statement. void indicates "The following statement is executed and the return value is ignored." Therefore, after void, any "single" statement that can be executed can occur. And the results of the execution are undefined. Of course, if you want, you can also "define undefined" with one of the following code.

1. A more complex method that uses an anonymous null function to perform a return

var undefined = function () {} ();

2. A more concise, but not easy to understand approach to code

var undefined = Void 0;void can also be used like a function, so void (0) is also legal. Sometimes, complex statements may not use the keyword form of void, but must be in the form of a function of void. For example:

Must use complex expressions in void () Form

void (I=1);

Or the following statement:

void (I=1, i++);

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.