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

An example of JavaScript function closure

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains the "JavaScript function closure example explanation", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "JavaScript function closure example explanation" bar!

First let's take a look at the prototype method:

1. The object method that is not defined by the prototype attribute is static and can only be called directly with the class name! In addition, the this variable cannot be used in this static method to call other properties of the object!

2. Object methods defined using the prototype property are non-static and can only be used after instantiation! Inside its method, you can this to reference other properties in the object itself!

Tips: all variables, without the var keyword, will be added to the properties of the global object by default!

Specifically, there are five common ways to write the simulation class:

The first way to write it: for a specific introduction, see the following code comments.

Function Circle (r) {this.r = r;} Circle.PI = 3.14159 prototype Circle.Pi belongs to the global variable * / Circle.prototype.area = function () {return Circle.PI * this.r * circle} / * the Circle method calls the prototype attribute so that the r attribute * / var c = new Circle (1.0) in the Circle method can be called with this; / * instantiate Circle*/ alert (c.area ())

The second way of writing: similar to the Java class, recommended!

Var Circle = function () {var obj = new Object (); / * first instantiate the Object method to call the obj.PI property and obj.area method * / obj.PI = 3.14159; obj.area= function (r) {return this.PI * r * r;} return obj;} var c = new Circle (); alert (c.area (1.0))

The third way of writing: the main idea is to instantiate an object and add properties and methods to the object.

Var Circle = new Object (); Circle.PI = 3.14159; Circle.Area = function (r) {return this.PI * r * r;} alert (Circle.Area (1.0))

The fourth way of writing: the writing method skips the step of new, and the computer is simple and clear. I personally recommend this method of writing!

Var Circle= {"PI": 3.14159, "area": function (r) {return this.PI * r * r;}}; alert (Circle.area (3.14159))

The fifth way of writing: this method is more or less the same as the first three, but I heard that this method is rarely used by people, so it is not recommended to use it!

Var Circle = new Function ("this.PI = 3.14159 countries this.area = function (r) {return rhabdomythis.Pi;}"); alert ((new Circle ()) .area (1.0))

Presumably all of you are wondering if it is simulated object-oriented, is it necessary to have encapsulation inheritance, getset method? The answer is yes, so let's look at the following code:

Encapsulation: after looking at the following code, we can clearly understand the use of encapsulation. If we remove the var in var name = "default";, what will be the result? The answer is the same as before, so you can see that a property without a var definition only acts as a global in its variable scope, and only the property is private in its scope. To define a private method, you need to assign it to a variable to act as a property. In addition, in this example, new can be added or not, and if not, it can be added after ().

Var person = function () {/ / variable scope is inside the function, var name = "default" cannot be accessed externally; return {getName: function () {return name;}, setName: function (newName) {name = newName;}} (); alert (person.name); / / direct access, the result is undefined alert (person.getName ()) Person.setName ("abruzzi"); alert (person.getName ())

Implementing classes and inheritance: the main idea of this example is to define a variable, assign a method to it, and achieve the effect of inheritance by putting the classes that need to inherit into it according to the prototype method.

Function Person () {var name = "default"; return {getName: function () {return name;}, setName: function (newName) {name = newName;}; / * encapsulated class Person * / var Jack = function () {}; / / inherited from Person Jack.prototype = new Person () / / add private method Jack.prototype.Say = function () {alert ("Hello,my name is Jack");}; var j = new Jack (); j.setName ("Jack"); j.Say (); alert (j.getName ()) Thank you for your reading, the above is the "JavaScript function closure example explanation" content, after the study of this article, I believe you have a deeper understanding of the JavaScript function closure example explanation of this problem, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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