In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to achieve JavaScript object-oriented support". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In JavaScript, we need to declare our own object type through a function:
/ /-
/ / formal code for type declaration of objects in JavaScript
/ / (in future documents, the "object name" is usually replaced by MyObject)
/ /-
Function object name (parameter table) {
This. Attribute = initial value
This. Method = function (method parameter table) {
/ / method implementation code
}
}
We can then create an instance of this object type with code like this:
/ /-
/ / create the form code of the instance
/ / (in future documents, the "instance variable name" is usually replaced by obj)
/ /-
Var instance variable name = new object name (parameter table)
Next, let's take a look at some concrete implementations and strange features of "objects" in JavaScript.
1)。 The five identities of function in the object-oriented Mechanism of JavaScript
-
"object name"? Such as MyObject ()? This function plays the following language roles:
(1) ordinary function
(2) Type declaration
(3) realization of type
(4) Class reference
(5) the constructor of the object
Some programmers, such as Delphi programmers, are used to separating type declaration from implementation. For example, in delphi, the interface section is used to declare types or variables, while the implementation section is used to write the implementation code of the type, or some function or code flow for execution.
In JavaScript, however, the declaration and implementation of types are mixed. The type (class) of an object is declared by a function, and the this.xxxx indicates the properties or methods that the object can have.
This function is also a "class reference". In JavaScript, if you need to identify the specific type of an object, you need to have a "class reference". ?? Of course, that's the name of this function. The instanceof operator is used to identify the type of instance. Let's take a look at its application:
/ /-
/ / Type recognition of objects in JavaScript
/ / Syntax: object instance instanceof class reference
/ /-
Function MyObject () {
This.data = 'test data'
}
/ / here MyObject () is used as a constructor
Var obj = new MyObject ()
Var arr = new Array ()
/ / here MyObject is used as a class reference
[xss_clean] ln (obj instanceof MyObject)
[xss_clean] ln (arr instanceof MyObject)
2)。 Implementation of reflection Mechanism in JavaScript
-
The reflection mechanism is implemented in JavaScript through for..in syntax. However, there is no clear distinction between "properties" and "methods" and "events" in JavaScript. Therefore, checking the type of an attribute is a problem in JS. The following code is a simple example of the use and attribute recognition of for..in:
/ /-
/ / for..in usage and attribute recognition in JavaScript
/ /-
Var _ r_event = _ r_event = / ^ [Oo] n.girls /
Var colorSetting = {
Method: 'red'
Event: 'blue'
Property:''
}
Var obj2 = {
A_method: function () {}
A_property: 1
Onclick: undefined
}
Function propertyKind (obj, p) {
Return (_ r_event.test (p) & & (obj [p] = = undefined | | typeof (obj [p]) = = 'function'))? 'event'
: (typeof (obj [p]) = = 'function')? 'method'
: 'property'
}
Var objectArr = ['window',' obj2']
For (var item0; 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.
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.