In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to analyze the way of writing JavaScript, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
At the beginning of this article, we will record some utility functions for writing classes. Some of the following lists are encountered at work, and some are collected from books or online.
Constructor + prototype directly assembles a class; the same constructor will assemble the same type
/ @ param {Function} constructor * @ param {Object} prototype * / function $class (constructor,prototype) {var c = constructor | | function () {}; var p = prototype | | {}; c.prototype = p; return c;} |
Use the constructor to generate the properties (fields) of the class instance, and the prototype object is used to generate the method of the class instance.
/ / Constructor function Person (name) {this.name = name;} / / prototype object var proto = {getName: function () {return this.name}, setName: function (name) {this.name = name;}} / / Assembly var Man = $class (Person,proto); var Woman = $class (Person,proto)
By this time, you have got two classes Man,Woman. And it's the same type. The tests are as follows:
Console.log (Man = = Woman); / / true console.log (Man.prototype = = Woman.prototype); / / true
Create an object and have a look
Var man = new Man ("Andy"); var woman = new Woman ("Lily"); console.log (man instanceof Man); / / true console.log (woman instanceof Woman); / / true console.log (man instanceof Person); / / true console.log (woman instanceof Person); / / true
Ok, everything is as we expected. But there is a problem. The result of the following code outputs false.
Console.log (man.constructor = = Person); / / false
This is unpleasant: you can see from the above code that man is indeed var man = new Man ("Andy") coming out of the Man class new, so the constructor of the object instance man should point to Man, but why did it backfire?
The reason is that the prototype of Person is rewritten in $class: c.prototype = p
OK, let's rewrite the $class slightly and hang the methods on the prototype of the constructor (instead of rewriting the prototype of the constructor), as follows:
Function $class (constructor,prototype) {var c = constructor | | function () {}; var p = prototype | | {}; / / c.prototype = p; for (var atr in p) {c.prototype [atr] = p [atr];} return c;} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.