JavaScript Object-oriented support (3)

Source: Internet
Author: User
Tags constructor inheritance key words object model client
javascript| objects

================================================================================
Qomolangma Openproject v0.9


Category: Rich Web Client
Key words: JS oop,js framwork, Rich Web client,ria,web Component,
Dom,dthml,css,javascript,jscript

Project launch: Aimingoo (aim@263.net)
Project team: Aimingoo, Leon (pfzhou@gmail.com)
Contributors: Jingyu (zjy@cnpack.org)
================================================================================

Eight, JavaScript object-oriented support
~~~~~~~~~~~~~~~~~~
Continued


2. JavaScript Object-oriented support
--------
Continued

5. Operations using the INSTANCEOF keyword
------
A instanceof keyword is provided in JavaScript to detect the type of an instance. This is in front of
It has been said about its "five identities". But the problem with instanceof is that it always enumerates the whole
The prototype chain is used to detect types (the principle of prototype inheritance is described in the "Constructs and Destructors" section), such as:
//---------------------------------------------------------
Problems in the use of instanceof
//---------------------------------------------------------
function MyObject () {
// ...
}

function MyObject2 () {
// ...
}
Myobject2.prototype = new MyObject ();

Obj1 = new MyObject ();
Obj2 = new MyObject2 ();

Document.writeln (obj1 instanceof MyObject, ' <BR> ');
Document.writeln (obj2 instanceof MyObject, ' <BR> ');

We see that obj1 and obj2 are examples of myobject, but they are different constructors that produce
Of Note that this is true in object-oriented theory: Because Obj2 is a subclass of MyObject
example, so it has the same characteristics as obj1. In the application this is one of the embodiment of obj2 polymorphism.

But even so, we have to face the question of how to know whether Obj2 and Obj1 are
What about the same type of instance? -that is, even the constructors are the same?

The INSTANCEOF keyword does not provide such a mechanism. One that provides the ability to achieve this kind of testing is
Object.constructor property. -but remember, it's more difficult to use than you think.

OK, here's the problem first. The constructor attribute has been concerned with the problem of "structure and destructor"
We'll talk about this later. "Prototype inheritance", "construction and destructor" are JavaScript's OOP
Main problems, core issues, and "fatal problems".


6). Null and undefined
------
In JavaScript, null and undefined once puzzled me. The following text is beneficial to
You recognize it more clearly (or make you more confused):
-NULL is a keyword; undefined is a property of the global object.
-NULL is an object (empty object with no attributes and methods); Undefined is the undefined class.
The value of the type. Try the following code:
Document.writeln (typeof null);
Document.writeln (typeof undefined);
-All objects in the object model are instances of object or its subclasses, with the exception of NULL objects:
Document.writeln (null instanceof Object);
-NULL "equivalent (= =)" In Undefined, but not "full equivalence (= = =)" In undefined:
Document.writeln (null = = undefined);
Document.writeln (null = = undefined);
-Null and undefined can be converted to false by type, but not equivalent to false:
Document.writeln (!null,!undefined);
Document.writeln (Null==false);
Document.writeln (Undefined==false);



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.