JS is special, there is no class concept, the object in JS is defined as: "A collection of unordered attributes, whose properties can contain basic values, objects or functions". js object can be directly understood as a hash table.each property or method of an object has a name, and each name is mapped to a value. Nothing more than a set of key-value pairs, where the values can be data or functions Each object is created from a reference type, which can be either a native type in JS or a developer-defined type. You can define an object like this
1 varper = {2Name: "LeBron",3Age:29,4Job: ' Software Engineer ',5Sayname:function(){6Console.debug ( This. Name);7 },8ChangeName:function(name) {9 This. Name =name;Ten } One A }; -Per.sayname ();//Output LeBron
The properties of an object have attribute properties (which can be associated with attributes such as private,static in Java), and there are two properties in ECMAScript: Data properties and Accessor properties Several ways that JavaScript creates objects
/** Factory mode Create object **///Factory mode demo1 use to create objects return objects by function Createperson (NAMESTR) {var per = new Object ();p Er.name = Namestr;per.sayname = function () {console.debug (this.name);}; per.tostring = function () {return this.name;} return per;} var p1 = Createperson (' Tom '), var p2 = Createperson (' Jack ');p 1.sayName ();p 2.sayName (); var PS = [];p s.push (p1);p S.push (p2), alert (PS), Console.debug (PS),///Factory mode Generation object Demo2 Create Object function Createdog using object literals ( NAMESTR) {var dog = {name:namestr,sayname:function () {console.debug (this.name);},tostring:function () {return This.nam e;}}; return dog;} var D1 = createdog (' Tom '), var d2 = Createdog (' Jack ');d 1.sayName ();d 2.sayName (); var PS = [];p s.push (d1);p S.push (D2); Console.debug (PS);
2. Creating an object using the constructor pattern(1). Create an Object(2). The scope of the constructor is given to the new object (so this points to the new object)(3). Execute the code in the constructor (add properties for this new object)(4). return new Objects
//构造器模式创建对象
functionPerson(name, age, job){//构造方法创建对象
//构造方法内部不需要显式的new对象
this.name = name;
this.age = age;
this.job = job;
this.sayName =function(){
console.debug(this.name);
console.debug(this.age);
console.debug(this.job);
}
//构造方法内部不需要显式的返回对象
}
//使用构造方法时,需要new操作创建对象
var per =newPerson(‘Lebron‘,27,‘SoftWareEngineer‘);
per.sayName();
each created object contains a constructor attribute, such as the per object created above, containing the constructor person (), see
If you create an object of two person types, find that their constructor properties are all pointing to the function person ()
var per1 =newPerson(‘Lebron‘,27,‘SoftWareEngineer‘);
console.debug(per1);
var per2 =newPerson(‘kobe‘,27,‘SoftWareEngineer‘);
console.debug(per2);
console.debug(per1.constructor ==Person);//true
console.debug(Person== per2.constructor);//true
The constructor property of an object is initially used to identify the object type, and it is better to detect the object type or use the instanceof operator, such as an instance of the Per1 object and an instance of object.creating a custom constructor means that its instance can be identified as a specific type in the future. 2.1 Use constructors as functionsconstructors are also functions, except that they are called differently. The following code shows several different ways of calling
functionPerson(name, age, job){
this.name = name;
this.age = age;
this.job = job;
this.sayName =function(){
console.debug(‘hi~~ ‘+this.name);
};
}
//使用构造函数的方式
var person =newPerson("Nicholas",29,"Software Engineer");
person.sayName();//"Nicholas"
//使用普通函数的方式,这个函数被添加为window对象的一个属性
Person("Greg",27,"Doctor");//adds to window
window.sayName();//"Greg"
window.name =‘lebron‘;
window.sayName();//lebron
//将函数绑定在一个对象上,并且设定了其作用域为这个绑定的对象
var obj =newObject();
Person.call(obj,"Kristen",25,"Nurse");
obj.sayName();//"Kristen"
obj.name =‘kobe‘;
obj.sayName();//kobe
? ssss
From for notes (Wiz)