js- objects (briefly)
(1) What is the object
In real life: Everything is an object, the object is a specific thing, there will be a specific thing and behavioral characteristics.
Objects in JavaScript is actually an abstract object in life,
Which may comprise a base attribute value of the object or function.
The object is a set of unordered values. We can think of JavaScript objects in key-value pairs, where the value can be data and functions.
(2) the nature: key-value pair;
(3) meaning: to store data and programming;
(4) created:
1. Create a new target by keyword:
Object obj = new new var (); // add the attribute obj.name = "ADMIN"; // Add Method obj.show = function () { the console.log ( "I" + this.name); };
2. Create an object by literal
var obj = { name : "zhangsan", age : "18", show : function () { console.log(''我是''+this.name+"年龄"+this.age); } };
3. Create objects through the constructor
function Person(name, age) { this.name = name; this.age = age; this.say = function(name) { console.log("你好: " + name + "!"); }; } // 创建两个Person对象 var person1 = Person("admin1", 18); var person2 = Person("admin2", 28);
(5) Properties and Methods
If all of a variable part of an object, then the variable can be referred to as an attribute of the object, property term is generally used to describe features of things
If all of a function belonging to an object, the function can be called a method of that object, the method is a verb, to describe the behavior and function of things
Properties and methods (6) to access the object
1. dot syntax:
To form: an object specific property or method;.
2. In bracket syntax:
Shaped as: target [variable] or object [ "specific property or method"];
Difference: the bracketing may be used as a variable name attribute, while not dot syntax;
brackets method may be used as a digital attribute name, not the dot syntax.
var obj = { name : "xiaoming", age : 18, 123:123, show : function () { console.log("我是"+this.name+"年龄"+this.age); } }; console.log(obj["name"]); // 输出:"xiaoming" console.log(obj.name); // 输出:"xiaoming" var age = "age"; console.log(obj[age]); // 输出:18 console.log(obj["123"]); // 输出:123
Traverse (7) of the object
obj = {var name: "Xiaoming", Age: 18 is, 123: 123, Show: function () { the console.log ( "I" + this.name + "Age" + this.age); } }; for ( I in obj var) { the console.log (I) is output // Key; }
(8) deleted object's properties (not the best):
delete object. property / method