Home > Web Front-end > JS Tutorial > What is prototype in js

What is prototype in js

(*-*)浩
Release: 2021-04-15 10:39:19
Original
8042 people have browsed it

Prototype in js represents the prototype of the function, and prototype represents a collection of attributes of a class; when an object of a class is generated through new, the attributes of the prototype object will become the attributes of the instantiated object; Inaccessible, that is, it becomes a private variable and private function.

What is prototype in js

The operating environment of this article: Windows 7 system, Dell G3 computer, javascript1.8.5.

The prototype object is an important mechanism for realizing object-oriented. Each function is also an object, and their corresponding class is function. Each function object has a sub-object prototype.

Prototype represents the prototype of the function, and prototype represents a collection of attributes of a class. When an object of a class is generated through new, the properties of the prototype object become the properties of the instantiated object.

This attribute is very useful: declare a common variable or function for a specific class.

If the variables and functions defined within the function do not provide an interface to the outside world, they will not be accessible from the outside, that is, they will become private variables and private functions.

function Obj(){               
                var a=0; //私有变量
                var fn=function(){ //私有函数                }
            }
Copy after login

Static variables and functions

After defining a function, the properties and functions added to it through "." can still be accessed through the object itself, but its instances cannot be accessed. Such variables and functions are called static variables and static functions respectively. Students who have used Java and C# can easily understand the meaning of static.

function Obj(){

            }
            Obj.a=0; //静态变量
            Obj.fn=function(){ //静态函数
            }
            console.log(Obj.a); //0
            console.log(typeof Obj.fn); //function

            var o=new Obj();
            console.log(o.a); //undefined
            console.log(typeof o.fn); //undefined
Copy after login

Related learning recommendations: js video tutorial

The above is the detailed content of What is prototype in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template