Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample Analysis of prototypes in js

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of the prototype in js, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Before we talk about the prototype of js, we must first understand Object and Function.

Object and Function are both built-in functions of JS, Object inherits itself, Funtion inherits itself, and Object and Function inherit each other, that is to say, Object and Function are both functions and objects.

Console.log (Function instanceof Object); / / trueconsole.log (Object instanceof Function); / / true

Object is an instance of Function, while Function is an instance of itself.

Console.log (Function.prototype); / / Object () {[native code]} console.log (Object.prototype); / /

Ordinary objects and function objects

Everything in JavaScript is an object, but there are differences between objects. It is divided into function objects and ordinary objects.

Function objects can create ordinary objects, ordinary objects can not create function objects, ordinary objects in the JS world of the lowest-level minions, there are no privileges.

All objects created through new Function are function objects, while others are ordinary objects (usually created through Object), which can be judged by typeof.

Function F1 () {}; typeof f1 / / "function" var o1 = new F1 (); typeof o1 / / "object" var O2 = {}; typeof o2 / / "object"

It should be noted here that the following two writing methods are the same.

Function f1 () {}; = = var f1 = new Function (); function f2 (amemb) {alert (aquib);} is equivalent to var f2 = new Function (amemb, "alert (aquib)")

Prototype, _ proto_, and construetor (constructor)

The following two sentences are also important.

1. Every function object has a prototype property, but ordinary objects do not.

There is another construetor under prototype that points to this function.

2. Each object has an internal property named _ proto_, which points to the prototype object of its corresponding constructor. The prototype chain is based on _ proto_.

All right, let's start with the code and examples, build a normal object, and we can see

1. O there is no prototype attribute.

2. O is an example of Object

3. _ _ proto__ of o points to prototype of Object

4. Object.prototype.constructor points to Object itself.

It can also continue to extend further.

Var o = {}; console.log (o.prototype); / / undefined console.log (o instanceof Object); / / true console.log (o.protoplast _ = Object.prototype) / / true console.log (Object = Object.prototype.constructor) / / true console.log (Object.prototype.constructor) / / function Object () console.log (Object.prototype.__proto__); / / null

Here is a function object, as you can see from the following example

1. Demo is a function object, F1 is an ordinary object

F1 is an example of Demo

3. The _ _ proto__ of demo's prototype prototype points to Object's prototype prototype, while the _ _ proto__ of Object's prototype prototyped points to null.

Function Demo () {}; var F1 = new Demo (); console.log (f1.prototype); / / undefined console.log (F1 instanceof Demo); / / true console.log (f1.protoplast = Demo.prototype); / / true console.log (Demo = Demo.prototype.constructor); / / true console.log (Demo.prototype.__proto__ = Object.prototype); / / true console.log (Object.prototype.__proto__); / / null

Prototype chain

In javascript, each object will generate a proto property internally. When we access an object property, if the object does not exist, we will go back to the object pointed to by proto and look for it layer by layer. This is the concept of javascript prototype chain.

F1.crude protoplasts _ = > Demo.prototype = > Demo.prototype.__proto__ = = > Object.prototype = = > Object.prototype.__proto__ = = > null

Everything in JS is an object, and everything is derived from Object, that is, the end of the prototype chain of everything points to null.

The above is all the content of the article "sample Analysis of prototypes in js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report