In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to add, delete, modify and check javascript objects. I hope you will get something after reading this article. Let's discuss it together.
What is the object?
Hey! Hey! Do you have to ask? Isn't it your girlfriend? Maybe you think I'm talking nonsense, but it is.
After reading the following definition, you will know whether I am right or not.
In real life: everything is an object, and the object is a concrete thing, which can be seen and touched. For example, a book, a car, a person can all be "objects", and a database, a web page, and a connection to a remote server can also be "objects".
In JavaScript, an object is an unordered collection of related properties and methods, and all things are objects, such as strings, numeric values, arrays, functions, and so on.
Objects are made up of properties and methods.
Attribute: the characteristic of a thing, represented by attributes in an object (a common noun).
Method: the behavior of things, represented by methods in objects (commonly used verbs)
Why do you need an object?
Why do you need an "object"? It's to dissolve your loneliness and loneliness, come on, man, don't be so superficial!
Find someone else and don't say anything, but she must have a role: she can make you more complete!
If you don't believe it, look:
When saving a value, you can use variables, and when saving multiple values (a set of values), you can use an array (compare the list in python). What if you want to keep a person's complete information?
For example, the personal information of "Zhang San" is saved in an array as follows:
Var arr= ['Zhang San', 'male', 168154]
This does store the data, but do you feel that he is not complete? Do you know what 168154 is? At this time, the role of the object is reflected.
Objects in js are represented by {} (compared to dictionaries in python), which makes the structure clearer and more powerful.
Var obj = {name:' Zhang San', sex:' male', height:168, weight:154, fun:function () {console.log ("I am Zhang San!") ;}}
The attribute or method inside we take the form of key-value pair. Key attribute name: value attribute value.
Separated by commas among multiple properties or methods.
The method colon is followed by an anonymous function.
There are three ways to create objects using the literal amount of objects to create objects.
* * literal amount of object: * * is the curly braces {}, which contains the properties and methods that express this specific transaction (object).
Var obj_1 = {}; / / created an empty object using the new keyword var obj_2 = new Object (); / / created an empty object using the constructor to create the object
Why do you need a constructor?
Var zhang = {name:' Zhang San', sex:' male', height:168, weight:154, fun:function () {console.log ("I am Zhang San!") ;} console.log (zhang); var li = {name:' Li Si', sex:' male', height:176, weight:154, fun:function () {console.log ("I'm Li Si!") ;}} console.log (li)
As you can see, we use the first two methods to create method objects, if we need more objects with the same properties and methods, it is very troublesome to create one by one.
So, we can use the function method to repeat the same code, and we call this function the constructor.
Constructor: a special function that is mainly used to initialize an object, that is, to assign an initial value to an object member variable, which is always used with the new operator. We can extract some common properties and methods from the object and encapsulate them in this function.
To put it popularly, a constructor abstracts some of the same properties and methods in our object into a function.
/ / the syntax format of the constructor: function constructor name () {this. Attribute = value; this. Method = function () {}} new constructor name (); function Star (name,age,sex) {this.name = name; this.age = age; this.sex = sex;} var hg = new Star ('Hu GE', 35 'male'); / / an object console.log (hg) is returned by calling the function; var ldh = new Star ('Liu Dehua', 50 'male')
This makes it much easier to create objects.
Note:
1. The first letter of the constructor name should be capitalized.
2. Our constructor does not need return to return the result.
3. We must use new to call the constructor.
4. We just need new Star () to call the function to create an object.
The execution process of new keyword
New does four things when it executes:
1. Create a new empty object in memory.
2. Let this point to this new object.
3. Execute the code in the constructor to add properties and methods to the new object.
4. Return this new object (so you don't need return in the constructor).
Addition, deletion and modification of object attributes (similar to py's dictionary) add var obj_1 = {} / / create an empty object / / add attributes and values obj_1.name = 'aniu'; obj_1.sex =' male'; console.log (obj_1)
Delete
Use the keyword delete
Var zhang = {name:' Zhang San', sex:' male', height:168, weight:154, fun:function () {console.log ("I am Zhang San!") ;}} delete zhang.weight; / / Delete the object's weight console.log (zhang)
Change
You can assign a new value to the attribute directly.
Var li = {name:' Li Si', sex:' male', height:176, weight:154, fun:function () {console.log ("I am Li Si!") ;}} li.sex = 'female'; / / modify the object's gender console.log (li)
Check
Attributes:
Console.log (li.name); / / Law I
Console.log (li ['sex']); / / method II
Call method:
Li.fun ()
Var li = {name:' Li Si', sex:' male', height:176, weight:154, fun:function () {console.log ("I am Li Si!") ;} console.log (li.name); / / console.log (li ['sex']); / / li.fun (); / / call method function Star (name,age,sex) {this.name = name; this.age = age; this.sex = sex; this.work = function (work) {console.log (work) }} var hg = new Star ('Hu GE', 35 'male'); / / an object hg.work ("Xianjian") is returned by calling the function; / / calling the method
In short, these crud operations are very similar to dictionaries in python and are easy to use.
Traversal object var li = {name:' Li Si', sex:' male', height:176, weight:154,} console.log (li.name); console.log (li.sex); console.log (li.height); console.log (li.weight)
It is troublesome to output the property values of an object like this, so you can use traversal.
For... The in statement is used to loop through the properties of an array or object.
Var li = {name:' Li Si', sex:' male', height:176, weight:154,} for (var k in li) {console.log (k); / / k variable output results in attribute name console.log (Li [k]); / / obj [k] output gets attribute value}
After reading this article, I believe you have a certain understanding of "how to add, delete, modify and query javascript objects". If you want to know more about it, welcome to follow the industry information channel, thank you for your reading!
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: 212
*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.