Easily win JavaScript (2)-object-oriented

Source: Internet
Author: User

Let's continue with the previous content to explore the object-oriented content of JavaScript. Today we will mainly learn the following knowledge points: Let's explain them one by one.

Objects in JS:

Javascript is a prototype-based object-oriented language without the concept of classes. Everything derives from a copy of an existing object.

There are two types of objects:

Function object. For example, the alert () function can use parameters to change the functions of such objects, such as alert ("ABC "). To print the output.

Object objects, which cannot be called as function objects and have fixed functions.

To improve efficiency, JavaScript provides the following built-in objects.

An object is a common basic object. You can use it to create a simple static object. Its abbreviation is {}.

var obj=new Object();var obj={};

A function is a copy of all objects that use parameters. It is also an object created when a function is defined in a script. Its abbreviation is function (){}.

var myFunction=new function(",");function myFunction(){}

Array is a set of special attributes and methods. For example, you can use its Length attribute to iterate over such objects. You can also use square brackets and serial numbers to access its attributes. It is abbreviated as [].

var arry=new Array();var arry=[];

String, Boolean, and number are used to represent strings, Boolean values, and numbers respectively.

In addition, there are math, date, Regexp and other built-in objects.

Inheritance in JS:

Inheritance is an important part of object-oriented systems. When creating your own object, you can extend or inherit the attributes and methods of the existing object. Inheritance provides a convenient way to reuse objects, so that you can focus fully on new code improvements.

Unlike traditional object-oriented languages, JavaScript is a prototype-based object-oriented language, which makes it impossible to extend the underlying class structure of another class from one class. Inheritance in JS is implemented by simply copying an object prototype to another object prototype. Let's take a look at this example:

<Title> inherit </title> <SCRIPT type = "text/JavaScript"> function Init () {var person = {}; person. getname = function () {alert ("person name") ;}; person. getage = function () {alert ("person age") ;}; // create another object var student ={}; student. getstunum = function () {alert ("Student stunum") ;}; student. getname = person. getname; person. getname = function () {alert ("person1 name") ;}; student. getage = person. getage; student. getname (); person. getname ();} window. onload = Init; </SCRIPT>

The result is as follows:

This shows that JS inheritance is implemented simply by copying methods from an object prototype to another object prototype.

Object member:

1. Two implementation schemes of Arrays

Ordered array and hash array (Key, value)

2. Object = associated array = attribute package = storage body

3. Only Members can access this operation. (JavaScript only has attributes and there is no method ). We believe that evil attributes are actually a key (attribute name) and value (attribute value ). Let's look at an example:

<script type="text/javascript">function init(){alert(document.body);}window.onload=init;</script>

The running result is:

Document. Body is an object in IE. This shows that document. body is actually an object, but the browser should display it according to W3C standards. Next Let's print another thing:

<script type="text/javascript">function init(){alert(document.getElementById);}window.onload=init;</script>

We printed one:

Actually, it is a function. when an object is passed to the alert function, all objects are object instances. Because the object class has a tostring method, that is, when an object is called in alert, A tostring method is actually called to obtain a string representation. The tostring method is derived from the basic object, this method will convert the object into a string, so this string is the name of this object type, so this result is displayed.

Window object: Most of the methods we write are subordinate to window objects. In this example:

function myFunction(message){alert(message);}

This method is actually a global window object method. Therefore, myfunction ("AAA") is equivalent to window. myfunction ("AAA ");

Scope chain: Let's look at an example.

<Title> scope chain </title> <SCRIPT type = "text/JavaScript"> function override () {var Alert = function (Message) {window. alert ('override' + message) ;}; alert ('alert '); window. alert ('window. alert ');} override (); alert ('alert from oulateral'); </SCRIPT> 

Let's analyze it. We defined an override, And then we defined an alert function. This is equivalent to the alert that overwrites the window itself; then we want to call the alert of the window itself, so we have to write this: window. Alert. Then, the alert below actually calls the function we defined. When we write window. Alert again, we still use the alert of window. The alert outside is also called by window. So there are several results:

This basic knowledge is not difficult. It is enough.

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.