Javascript learning notes (functions, objects, inheritance)

Source: Internet
Author: User

JavaScript is executed in segments. The definition function statements in a piece of code are executed first, which seems to be a bit like the compilation concept of static language. Therefore, this feature is also called "pre-compilation" of JavaScript ".
 
A function is an object. It only adds a bracket () operator to an object. This operator is used to execute the function logic.
 
Call method example
Function WhoAmI () // defines a function WhoAmI
{
Alert ("I'm" + this. name + "of" + typeof (this ));
};
WhoAmI (); // this is the global object of this current code, which is the window object in the browser.
The name attribute is an empty string. Output: I'm of object
Var BillGates = {name: "Bill Gates "};
BillGates. WhoAmI = WhoAmI; // use the function WhoAmI as the method of BillGates.
BillGates. WhoAmI (); // at this time, this is BillGates. Output: I'm Bill Gates of obje
Ct
Var SteveJobs = {name: "Steve Jobs "};
SteveJobs. WhoAmI = WhoAmI; // use the function WhoAmI as the SteveJobs method.
SteveJobs. WhoAmI (); // at this time, this is SteveJobs. Output: I'm Steve Jobs of o
Bject
WhoAmI. call (BillGates); // directly use BillGates as this and call WhoAmI. Output: I'm B
Ill Gates of object
WhoAmI. call (SteveJobs); // directly use SteveJobs as this and call WhoAmI. Output: I'
M Steve Jobs of object
BillGates. WhoAmI. call (SteveJobs); // uses SteveJobs as this, but calls BillGates
WhoAmI method. Output: I'm Steve Jobs of object
SteveJobs. WhoAmI. call (BillGates); // uses BillGates as this, but calls
WhoAmI method. Output: I'm Bill Gates of object
WhoAmI. WhoAmI = WhoAmI; // set the WhoAmI function to its own method.
WhoAmI. name = "WhoAmI ";
WhoAmI. WhoAmI (); // at this time, this is the WhoAmI function itself. Output: I'm WhoA
MI of function
({Name: "nobody", WhoAmI: WhoAmI}). WhoAmI (); // create an anonymous object temporarily and
Call the WhoAmI method after setting the attributes. Output: I'm nobody of object
 
 
In JavaScript, prototype not only allows objects to share their wealth, but also allows prototype to search for the original
Nature, so that the heritage of the elders can be passed on from generation to generation. When you read a property from an object or call a method, if the object itself
If such an attribute or method does not exist, it will search for the prototype object associated with it. If prototype does not exist, it will go to p
Rototype is searched for by its predecessor prototype until the process of finding or tracing is completed.
 
1 function Person (name) // base class Constructor
2 {
3 this. name = name;
4 };
5
6 Person. prototype. SayHello = function () // Add method to prototype of the base class Constructor
7 {
8 alert ("Hello, I'm" + this. name );
9 };
10
11 function Employee (name, salary) // subclass Constructor
12 {
13 Person. call (this, name); // call the base class Constructor
14 this. salary = salary;
15 };
16
17 Employee. prototype = new Person (); // create a base class object as the prototype of the subclass prototype,
Interesting here
18
19 Employee. prototype. ShowMeTheMoney = function () // Add the constructor pr to the subclass.
How to add ototype
20 {
21 alert (this. name + "$" + this. salary );
22 };
23
24 var BillGates = new Person ("Bill Gates"); // create a BillGates object for the base class Person
25 var SteveJobs = new Employee ("Steve Jobs", 1234); // create a subclass of Employee
SteveJobs object
26
27 BillGates. SayHello (); // call the prototype method directly through an object
21
28 SteveJobs. SayHello (); // directly call the prototype method of the base class through the subclass object!
29 SteveJobs. ShowMeTheMoney (); // directly call the prototype method of the subclass through the subclass object
30
31 alert (BillGates. SayHello = SteveJobs. SayHello); // display: true, indicating prototyp
E is shared.

 

 

This article is from mayi-hetu"
 

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.