Aunt Lin basics of JavaScript (III): JavaScript programming (1) Objects

1. Object briefly with some caveats

  JavaScript has few simple data types: numeric, string, boolean, null values, and undefined value. In addition to all the remaining values (including arrays, functions, and even regular expression) is an object. Numbers, strings and Boolean values of the surface is the object (because they have a method), but they are immutable, but in reference to a JavaScript number, string, or Boolean value, by calling the new Number (), new String ( ) and new Boolean () constructor in order to convert a number, string, or Boolean object that is obtained own method inherited from the prototype. The basic data types can not be changed, so when there is no pointer to point to it, it takes up in memory resources automatically released, and the object was never passed by reference , change the value of the object, it changed it in the value of memory.

  Note: In theory, a null value is null value, should not be detected as an object, but in fact, due to design errors in JavaScript, typeof null === "object" is a fact.

Therefore, when testing a null value, try to use other methods such as:! Null === true. For example, to detect whether a newly acquired DOM object gets to normal, we should use the following code:

1 var element = document.getElementById("elementID");
2 if(element){}
3 // 或者if(!element){}

Instead:

1 var element = document.getElementById("elementID");
2 if(typeof element !== "object") {}
3 // 或者if(typeof element === "object") {}

  Speaking above, because typeof null === "object", so that the following code, even when the element is not normally acquired (i.e., is null), if the statement will act in accordance with the normal result acquisition.

 

2. object attributes

  The meaning of existence of the object is that it can have many properties and methods help us to express logic. Attribute of the object may be an arbitrary value can be a number, a string, boolean, null value, or may be a function of the object (of course, the function is essentially the object). Outputting a property, if it has been defined, but a null value, it will output the corresponding null, even if it has not been defined, the corresponding output of undefined.

  We first define an object with a literal, convenient later tests:

1 var game = {
2     name: "Mini Warior",
3     id: 201907271234,
4     qualification: {
5         type: "test"
6     }
7 }

 

  We operate on the properties of the object in general is CURD, the most common is to modify the update and retrieval of values.

  Update modify an object's properties, we can directly use the assignment statement. Retrieves and acquires the general properties of an object in two ways: first, because it is more compact and more readable and the most commonly used:  var GAMENAME = game.name;  we call (here points ↘) . representation; second, obtaining the target value as corresponding to the same array:  var GAMENAME Game = [ "name"];  . When retrieving get property values, we have to take into account the situation of the property does not exist, this time we can skillfully operators fill default values:  var GameName = game.name || "Mini Warior";  . This time if game.name acquisition value is null or undefined value, gameName will be assigned an initial value you want, without affecting the subsequent code to run. If we try to undefined values from the properties, such as  var gameQualiPlayer = game.qualification.player;  , JavaScript will throw a "TypeError" wrong and we can skillfully use the && operator:  var gameQualiPlayer = game.qualification && game.qualification.player;  avoid errors, but the result is still out a undefined value.

  When dealing with some unwanted attributes (for example, property on the prototype chain), we use the typeof keywords in addition to outside, there is a way to use Object.prototype.hasOwnProperty () method. This method returns a boolean value parameter is filled in the current object. The more common usage is in use for in the statement when traversing an object, for in default statement will traverse the prototype chain to the property. If the property on the prototype chain is modified other people in the development process, and you do not know, inexplicable error occurs. Therefore, using this method will be more safety in use when the statement for:

 1 var player = {
 2     name: 'Lyn',
 3     age: 19,
 4     gender: 'male'
 5 };
 6 
 7 var playerPropertyName = [];
 8 var playerProperty = [];
 9 
10 for(var key in player) {
11     playerPropertyName.push(i);
12     playerProperty.push(player[i]);
13 }

  The above code key player in the attribute is stored playerPropertyName array, the value of the attribute is stored player playerProperty array. The following new properties added to the player in the prototype chain, using Object.prototype.hasOwnProperty () Filter:

 1 var player = {
 2     name: 'Lyn',
 3     age: 19,
 4     gender: 'male'
 5 };
 6 player.prototype.playGame = 'Mini Warior';
 7 
 8 var playerPropertyName = [];
 9 var playerProperty = [];
10 
11 for (var key in player){
12     if(player.hasOwnProperty(key){
13         playerPropertyName.push(i);
14         playerProperty.push(player[i])'
15     }
16 }

  Thus, during traversal, will not playGame keys and values ​​to the corresponding stored inside the array.

  Some enumerable properties of the object, and some non-enumerable. Like when traversing an object for in this statement we've used above all enumerable property or method will be affected, so we mention you can use to filter typeof keywords or hasOwnProperty method. But we know, JavaScript is an Array object, we create an array, output it, you will find the array index is key, value is value, there is a non-enumerable length attribute (and of course the prototype). In this case, we use the for in statement will not affect the length property. for in the statement is not only easy to traverse method and prototype property, it can not control the order in the attribute name, so we traverse best practice should use common for statement instead of for in the statement (of course, when talked about closure, we will be found for the statement is also not perfect, but at least it is in most cases better than for in traversing).

  As to delete object properties, we will use the delete operator, the point to note is that it will not touch the prototype chain in the content, but to delete this object's properties, the properties may make the prototype chain emerges: Suppose we there is a player object which has a property nickname: "Lyn", has a property of its prototype nickname: "Ben", after  Delete player.nickname;  , the output value player.nickname Ben.

  Objects will help us write scalable, reusable high-quality JavaScript libraries. But of course, there are several important concepts and objects are created prototype chain of the object, it is worth us into a new blog post about.

Guess you like

Origin www.cnblogs.com/BlogOfMotherLyn/p/11247142.html