Javascript object-oriented Learning 1 function object

Source: Internet
Author: User

JavaScript programming has been around for a while, but there are still a lot of basic concepts that seem to be understandable. If you do not understand them, correct them.

1: function and Function

Code
Alert (function );
// Function function (){
// [Native code]
// }
Alert ( Typeof Function ); // Function
Alert (Function Instanceof Object ); // True

Function Fun (){};
Alert ( Typeof Fun ); // Function
Alert (fun. Constructor = Function ); // True
Alert (fun Instanceof Function ); // True
Alert (fun Instanceof Object ); // True

The built-in function of the function system, which is used to create user-defined functions. And they all "inherit" from objects.

2: function and Object

Code
Function Class (){};

Alert ( Typeof Class ); // Function
Alert (class. Constructor = Function ); // True
Alert (Class Instanceof Function ); // True
Alert (Class Instanceof Object ); // True
Alert ( Typeof Class. Prototype ); // Object

VaR C1 =   New Class ();
Alert ( Typeof C1 ); // Object
Alert (c1.constructor = Class ); // True
Alert (C1 Instanceof Class ); // True
Alert (C1 Instanceof Function ); // False
Alert (C1 Instanceof Object ); // True
Alert ( Typeof C1. _ PROTO __); // In IE, undefined Firefox is an object.
Alert (C1. _ PROTO __ = Class. Prototype ); // In IE, flase Firefox is true.

A function is an instance of a function. It inherits from an object. Besides the features of an object, it also has
1) You can perform the new operation to simulate some object-oriented functions. The new operation returns an object. It is an instance of constructors and object objects.
2) Three steps for the new class () Operation
A) var C1 = new object
B) Copy all the attributes and methods of the original function class to the new C1.
C) C1. _ PROTO _ = Class. Prototype
3) In C1, point this to C1
// _ PROTO __is not visible in IE, but corresponding hidden values should exist.

3: About instanceof in Javascript
Interesting to see an example in http://www.cnblogs.com/bmrxntfj/archive/2008/07/17/829833.html
Code
Function Class1 (){};
Function Class2 (){};
Class2.prototype =   New Class1 ();
Function Class3 (){};
Class3.prototype =   New Class2 ();
Function Class4 (){};
Class4.prototype =   New Class3 ();
Function Class5 (){};
Class5.prototype =   New Class4 ();

VaR OBJ =   New Class4 ();

// Test the normal inheritance relationship
Alert (OBJ Instanceof Class5 ); // False
Alert (OBJ Instanceof Class4 ); // True
Alert (OBJ Instanceof Class3 ); // True
Alert (OBJ Instanceof Class2 ); // True
Alert (OBJ Instanceof Class1 ); // True

Class3.prototype =   New Class5 (); // Change the inheritance relationship

// Test the inherited relationship after the change
Alert (OBJ Instanceof Class5 ); // False
Alert (OBJ Instanceof Class4 ); // True
Alert (OBJ Instanceof Class3 ); // False
Alert (OBJ Instanceof Class2 ); // True is still true
Alert (OBJ Instanceof Class1 ); // True is still true

Use the followingCodeThe test is only valid in Firefox.

 

Code
VaR _ PROTO = OBJ. _ PROTO __;

While (_ PROTO ){
If (_ PROTO = Class1.prototype ){
Alert ( " Class1 " );
}
Else   If (_ PROTO = Class2.prototype ){
Alert ( " Class2 " );
}
Else   If (_ PROTO = Class3.prototype ){
Alert ( " Class3 " );
}
Else   If (_ PROTO = Class4.prototype ){
Alert ( " Class4 " );
}
Else   If (_ PROTO = Class5.prototype ){
Alert ( " Class5 " );
}
Else   If (_ PROTO = Object. Prototype ){
Alert ( " Object " );
} Else {
Alert ( " Unknow " );
Alert (_ PROTO. constructor );
}
_ PROTO = _ PROTO. _ PROTO __;
}

Normal inheritance relationship
Class4-> class3-> class2-> class1-> Object

Inheritance relationship after change
Class4-> unknow-> class2-> class1-> Object

Instanceof compares nodes on the _ PROTO _ chain one by one. If an equal node is found, true is returned; otherwise, false is returned.

The _ PROTO. constructor of the unknow object is displayed as "function class1 (){}"
However, except for objects, class4 ~ The _ PROTO. constructor of class1 is "function class1 (){}"
I still don't quite understand this, but I really don't understand it. If you know it, give some advice.


Refer:
Http://www.javaeye.com/topic/155109
Http://www.cnblogs.com/bmrxntfj/archive/2008/07/17/829833.html
Http://blog.csdn.net/nksongzz/archive/2008/06/25/2585196.aspx

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.