JavaScript prototype prototype understanding

Source: Internet
Author: User

First we must understand JS has several data types?

String number Boolean Object Null Undefined

Many other objects can be derived under Object objects

such as function Date Math array, etc.

I drew a picture of myself in a simple and rough way:

Red represents the object black represents the prototype object

Here are just a few points to keep in mind:

1, what are the objects that have prototype? function Array String Math object function etc some built-in objects

var student={
Name: ' Student ',
Grade: ' Class three ',

}
var b=new Object ();
Console.log (b.prototype);//undefined
var c= ' AAA ';
Console.log (c.prototype);//undefined
Console.log (String.prototype);//String common methods such as IndexOf

Why is it undefined because they're the objects that instantiate an object that is instantiated from an object is not a prototype object.

Therefore, you cannot use an instantiated object to. prototype

2,__proto__ is a property owned by each object, including an instantiated object, and the function is to find out which object the object derives from.

Console.log (c.prototype);//undefined
Console.log (c.__proto__);//string

Practice

Function Show () {
Alert (' Hello ');
}
Console.log (show.prototype);//object
Console.log (show.prototype.constructor);//function Show () {alert (' Hello ');}
Console.log (show.__proto__);//function () {}
Console.log (show.__proto__.__proto__);//object{}
Console.log (show.__proto__.__proto__.__proto__);//null
Array.prototype.show=function () {
Alert (' Hello ');
}
var arr=[1,2,3];
Arr.show ();//Instantiate an array also has a show method

JavaScript prototype prototype understanding

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.