node. JS JavaScript understands prototype inheritance

Source: Internet
Author: User

Util.inherits

Util.inherits (constructor, Superconstructor) is a function that implements prototype inheritance between objects.

The object-oriented nature of JavaScript is prototype-based and differs from common class-based ones. JavaScript does not provide language-level attributes for object inheritance, but is implemented through prototype replication.

varUtil = require (' util '));functionBase () { This. Name = ' base ';  This. Base = 1991;  This. SayHello =function() {Console.log (' Hello ' + This. Name); };} Base.prototype.showName=function() {Console.log ( This. name);};functionSub () { This. Name = ' Sub ';}
Util.inherits (Sub, Base);
varObjbase =Newbase (); Objbase.showname (); Objbase.sayhello (); Console.log (objbase);
varObjsub =NewSub (); Objsub.showname ();//Objsub.sayhello (); no accessConsole.log (objsub);

Util.inspect

Util.inspect (Object,[showhidden],[depth],[colors]) is a method that converts any object to a string, typically for debugging and error output. It accepts at least one parameter, object, which is to be converted.
ShowHidden is an optional parameter, and if the value is true, more hidden information will be output.
Depth represents the maximum number of recursive layers, and if the objects are complex, you can specify the number of layers to control how much output information is.

If you do not specify depth, the default is 2 levels, which is specified as null to completely traverse the object with an unlimited number of recursive layers.
If the color value is true, the output format will be encoded in ANSI color, typically used to display a more beautiful effect on the terminal.
In particular, Util.inspect does not simply convert an object to a string, even if the object defines the ToString method and does not invoke the

Util.isarray () Util.isregexp () util.isdate () Util.iserror () Util.format () util. Debug ()

 

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.