Java Programmer's JavaScript learning notes (5--prototype and object built-in methods)

Source: Internet
Author: User
Tags object object

plan to complete this note in the following order:
  1. Idea.
  2. Property replication and inheritance.
  3. This/call/apply.
  4. Closed Packet/getter/setter.
  5. Prototype
  6. Object-oriented simulation.
  7. The basic mechanism of jquery.
  8. jquery selector.
  9. jquery Tool method.
  10. jquery-extends at the class level.
  11. jquery-extends at the "object" level.
  12. jquery-extension Selector.
  13. JQuery UI.
  14. Extend the jquery UI.
This is the 5th note, a chat about the properties and methods of prototype, built-in object objects, and object objects.

1, prototypein the previous article, we learned how to implement the Control object property's access rights, implemented the object property encapsulation.

In the 1th chapter, we extend the function prototype by extending it to all objects created by the function. Inheritance is also implemented using the prototype property.

Combining the previous studies, we'll look at how to implement similar Java code functions.

public class Uiobject{private String _author = ' Liuhailong ';p rivate string _utype  = ' UiObject ';p rivate string _uname< c1/>= ' UiObject ';p ublic UiObject (String name) {this._uname = name;} public void Setuname (name) {this._uname = name;} public string Getauthor () {return this._author;} public string Getutype () {return this._utype;} public string Getuname () {return this._uname;} }

Try writing the following code:

function UiObject (uname) {    var _uname = uname | | ' Rootuiobject ';}  Uiobject.prototype = {      _utype: "UiObject",      _author: ' Liuhailong ',        get Utype () {return this._utype;},    Get author () {return this._author;},        get Uname () {return this._uname;},    set uname (uname) {this._uname = uname;},        render:function () {          console.log (' render me! ');      }  }  var uiroot = new UiObject (); uiroot.uname = ' new name '; uiroot.utype = ' new type '; Uiroot.author = ' new Autho '; Console.log (UI Root.uname); Output:new Nameconsole.log (Uiroot.utype); Output:UiObjectconsole.log (Uiroot.author);//Output:liuhailong
Prototype and closures are used in the code to achieve results in terms of output.

This code will serve as a template for my future implementation class, and as I accumulate, I will try to update the template to find best practices for defining classes in JavaScript.


The importance of prototype provides strong support for basic type definitions for a class object (Function) in JavaScript.

And it is powerful because it provides the function function and its convenient and easy to use extension function through prototype.

2. Built-in object objects

Look at the following code:

function UiObject () {}var UI = new UiObject (); Console.log (ui.tostring ());   Output: [object Object]var o = {};console.log (o.tostring ()); Output: [Object Object]
Two "empty" objects are defined, and the ToString method is called to return the value, should not return undefined?

I believe the Java programmer smiled and guessed what had happened.


Through Firefox debugging observation, two empty objects are really "empty". What's going on?


If only the own attribute is counted, and two objects are indeed "empty", their prototype should be assigned by default.

Remember the 4 attributes of the property we talked about last? One of the features is whether it can be traversed. such as the prototype attribute, it cannot be traversed.


Instead of using IE debugging, you see the prototype property of two objects (gray, which should be due to non-traversal) and the internal structure of the default prototype.

As shown in.


Observing the types of each object, you can see that the prototype type of the UiObject, UI prototype is object, and the type of O itself and prototype is object.

Object has revealed the true face of the Buddha, expanding to see its internal properties, if any, and methods.

3. Properties and methods of object

The types you can see are function, some can be overridden by assignments, and some cannot.

Did not see the previous article mentioned Object.defineproperty, also did not see Object.create (), may be the browser to ECMAScript 5 support for different reasons, or it may be the debugger for some reason is not listed.



You can ask the mother to understand the properties and methods of object, it should be noted that the above list is not a complete set.

Java Programmer's JavaScript learning notes (5--prototype and object built-in methods)

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.