Javascript learning 3 -- javascript object-oriented (I)

Source: Internet
Author: User

The same is true for objects in JS. everything is object, which is the basis of everything. An object is a collection of variables and functions. Objects are generally derived from classes, and Classes define the attributes and methods of objects. If all your scripts are interactive operations between objects, you can say that this script is an object-oriented script. JavaScript is a prototype-based object-oriented language. It does not have the concept of classes. Everything derives from a copy of an existing object. Objects in JS are divided into two types: 1. Function objects. For example, the alert () Function can use parameters to change the functions of such objects, such as alert ('kfkf'); 2. Object objects, such objects cannot be called as Function objects and have fixed functions. Javascript inheritance in JS is prototype-based object-oriented, which leads to the inability to extend the underlying class structure of another class from one class. Inheritance in JS is implemented by simply copying methods from an object prototype to another object prototype. Let's take a look at the following example: [javascript] <script type = "text/javascript"> function init () {var person = new Object (); person. getName = function () {alert ('person name');} person. getAge = function () {alert ('person age');} // create another Object var student = new Object (); // create a method student. getStuNum = function () {alert ('student num') ;}; // inherit the getName Method student of person. getName = person. getName; // redefines the getName method of person. getName = function () {Alert ('person1 name');} student. getAge = person. getAge; student. getName (); person. getName ();} window. onload = init; </script>: In this example, we can understand the above content. Object member object = associated array = property package = storage body in javascript only objects access operation prototype/Object Property package prototype only exists in function, after an object is created, creates an empty prototype object by default. Since the object is an attribute package, the prototype is also an attribute package. When reading in the prototype, read from the prototype chain, and write to yourself when writing. See the following example: [javascript] <script type = "text/javascript"> function object2 () {}; function object3 () {}; object3.prototype = new object2 (); object. prototype. foo = function () {alert ('object. prototype. foo ') ;}; aObj = new object3 (); aObj. foo (); object2.prototype. foo = function () {alert ('object2. prototype. foo ');}; aObj. foo (); </script> running result: First Time: object. prototype. foo: object2.prototype. foo can help understand: the window object overwrites the scope chain. See the following example: [javascript] <script type = "text/javascript"> function override () {var alert = function (message) {window. alert ('override' + message) ;}; alert ('alert '); window. alert ('window. alert ');} override (); window. alert ('window. alert from outside '); </script>

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.