In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "JavaScript flexibility example Analysis". In daily operation, I believe many people have doubts about JavaScript flexibility example analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts of "JavaScript flexibility example Analysis". Next, please follow the editor to study!
First, deploy an add method on the Number.prototype object.
Number.prototype.add = function (x) {return this + x;}
The above code defines an add method for an instance of Number. If you are not familiar with this way of writing, it is recommended to read my "JavaScript object-oriented programming" first. )
Because an instance of Number is a numeric value, if you call a method on a numeric value, the value is automatically converted to an instance object, so you get the following result.
8 ['add'] (2) / / 10
In the above code, the calling method is written as 8 ['add'] instead of 8. Add because the point after the value is interpreted as a decimal point rather than a point operator.
By placing the value in parentheses, you can use the point operator to call the method.
(8) .add (2) / / 10
In fact, there is another way to write it.
8..add (2) / / 10
The * points in the above code are interpreted as decimal points, and the second point is interpreted as the dot operator. For the sake of semantic clarity, I will uniformly use parentheses below.
Because the add method returns a numeric value, it can be chained.
Number.prototype.subtract = function (x) {return this-x;}; (8) .add (2) .subtract (4) / / 6
The above code deploys the subtract method on an instance of the Number object, which can be called chained with the add method.
If you use square brackets to call a property, it can be written in an odd way.
8 ["add"] (2) ["subtract"] (4) / / 6
We can also deploy more complex methods.
Number.prototype.iterate = function () {var result = []; for (var I = 0; I
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.