In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of js packaging, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
The class in JS is actually a function, and it is also the constructor of this class. New creates an instance of this class, and the object out of new has properties and methods. The js encapsulation method is also a special object.
Js encapsulation method of class
The method of initializing the instance in the constructor (that is, writing the method directly in the constructor and instantiating new) is not recommended and consumes memory (each instantiation is repetitive and takes up more memory, which is neither environmentally friendly nor efficient).
All instances are common. Creating multiple instances will not generate a new function. It is recommended to define the instance in the prototype of the class.
The methods in prototype are common to all instances.
Emulate the js wrapper class
(function () {
/ /
) ()
VarId=function (I) {
This.id=document.getElementById (I)
}
Window.$=function (I) {
ReturnnewId (I)
}
Console.log ($('main'))
FunctionCat (name,color) {
This.name=name
This.color=color
}
Varcat1=newCat ('big hair', 'yellow')
Varcat2=newCat ('Ermao', 'black')
Cat.prototype.a='aaa'
Cat.prototype.type=' cats'
Cat.prototype.eat=function () {
Alert ('eat mice')
}
Cat1.eat ()
Cat2.eat ()
Console.log (cat1.name)
Console.log (cat2.color)
/ / cat1 and cat2 automatically contain a constructor attribute pointing to their constructor.
Console.log (cat1.constructor==Cat)
Console.log (cat2.constructor==Cat)
/ / Javascript also provides an instanceof operator to verify the relationship between prototype objects and instance objects.
Console.log (cat1instanceofCat)
Try {
Console.log (ainstanceofCat)
} catch (e) {
Console.log (e)
}
The so-called "constructor" is actually an ordinary function, but the this variable is used internally. Using the new operator on the constructor generates the instance, and the this variable is bound to the instance object.
Javascript states that each constructor has a prototype property that points to another object. All properties and methods of this object are inherited by the instance of the constructor.
Thank you for reading this article carefully. I hope the article "sample Analysis of js package" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.