JS Object-oriented

Source: Internet
Author: User

 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 
  1. //构造器模式创建对象
  2. functionPerson(name, age, job){//构造方法创建对象
  3. //构造方法内部不需要显式的new对象
  4. this.name = name;
  5. this.age = age;
  6. this.job = job;
  7. this.sayName =function(){
  8. console.debug(this.name);
  9. console.debug(this.age);
  10. console.debug(this.job);
  11. }
  12. //构造方法内部不需要显式的返回对象
  13. }
  14. //使用构造方法时,需要new操作创建对象
  15. var per =newPerson(‘Lebron‘,27,‘SoftWareEngineer‘);
  16. 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 () 
  1. var per1 =newPerson(‘Lebron‘,27,‘SoftWareEngineer‘);
  2. console.debug(per1);
  3. var per2 =newPerson(‘kobe‘,27,‘SoftWareEngineer‘);
  4. console.debug(per2);
  5. console.debug(per1.constructor ==Person);//true
  6. 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  
  1. functionPerson(name, age, job){
  2. this.name = name;
  3. this.age = age;
  4. this.job = job;
  5. this.sayName =function(){
  6. console.debug(‘hi~~ ‘+this.name);
  7. };
  8. }
  9. //使用构造函数的方式
  10. var person =newPerson("Nicholas",29,"Software Engineer");
  11. person.sayName();//"Nicholas"
  12. //使用普通函数的方式,这个函数被添加为window对象的一个属性
  13. Person("Greg",27,"Doctor");//adds to window
  14. window.sayName();//"Greg"
  15. window.name =‘lebron‘;
  16. window.sayName();//lebron
  17. //将函数绑定在一个对象上,并且设定了其作用域为这个绑定的对象
  18. var obj =newObject();
  19. Person.call(obj,"Kristen",25,"Nurse");
  20. obj.sayName();//"Kristen"
  21. obj.name =‘kobe‘;
  22. obj.sayName();//kobe
                                    ? ssss



From for notes (Wiz)



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.