The editor below will bring you an old-fashioned object-oriented idea of javascript. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
The three basic characteristics of object-oriented
Encapsulation (encapsulation of relevant information (regardless of data or methods) )The ability to be stored in an object)
Inheritance (the ability to obtain the properties and methods of a class from another class (or multiple classes))
Polymorphism (an object in different situations Various forms under)
Define a class or object
The first one: based on Object object
var person = new Object(); person.name = "Rose"; person.age = 18; person.getName = function () { return this.name; }; console.log(person.name);//Rose console.log(person.getName);//function () {return this.name;} console.log(person.getName());//Rose
Disadvantages: Multiple objects cannot be created.
The second type: based on literal method
var person = { name : "Rose", age : 18 , getName : function () { return this.name; } }; console.log(person.name);//Rose console.log(person.getName);//function () {return this.name;} console.log(person.getName());//Rose
Advantages: relatively clear search object contains Properties and methods;
Disadvantages: Multiple objects cannot be created.
The third type: factory mode
Method 1:
function createPerson(name,age) { var object = new Object(); object.name = name; object.age = age; object.getName = function () { return this.name; }; return object; } var person1 = createPerson("Rose",18); var person2 = createPerson("Jack",20); console.log(person1.name);//Rose console.log(person2.name);//Jack console.log(person1.getName === person2.getName);//false//重复生成函数,为每个对象都创建独立的函数版本
Advantages: Multiple objects can be created;
Disadvantages: Generate function getName() repeatedly, creating an independent function version for each object.
Method 2:
function createPerson(name,age) { var object = new Object(); object.name = name; object.age = age; object.getName = getName; return object; } function getName() { return this.name; } var person1 = createPerson("Rose",18); var person2 = createPerson("Jack",20); console.log(person1.name);//Rose console.log(person2.name);//Jack console.log(person1.getName === person2.getName);//true//共享同一个函数
Advantages: multiple objects can be created;
Disadvantages: semantically In other words, the function getName() is not very similar to the method of the Person object, and the recognition is not high.
The fourth way: constructor method
Method 1:
function Person(name,age) { this.name = name; this.age = age; this.getName = function () { return this.name; } } var person1 = new Person("Rose",18); var person2 = new Person("Jack",20); console.log(person1.name);//Rose console.log(person2.name);//Jack console.log(person1.getName === person2.getName); //false//重复生成函数,为每个对象都创建独立的函数版本
Advantages: Multiple objects can be created;
Disadvantages: Generate function getName() repeatedly, creating an independent function version for each object.
Method 2:
function Person(name,age) { this.name = name; this.age = age; this.getName = getName ; } function getName() { return this.name; } var person1 = new Person("Rose",18); var person2 = new Person("Jack",20); console.log(person1.name);//Rose console.log(person2.name);//Jack console.log(person1.getName === person2.getName); //true//共享同一个函数
Advantages: multiple objects can be created;
Disadvantages: semantically In other words, the function getName() is not very similar to the method of the Person object, and the recognition is not high.
Fifth: Prototype method
function Person() { } Person.prototype.name = 'Rose'; Person.prototype.age = 18; Person.prototype.getName = function () { return this.name; }; var person1 = new Person(); var person2 = new Person(); console.log(person1.name);//Rose console.log(person2.name);//Rose//共享同一个属性 console.log(person1.getName === person2.getName);//true//共享同一个函数
Disadvantages: It omits passing initialization to the constructor Parameters, which brings inconvenience to certain programs; in addition, the most important thing is that when the object's attribute is a reference type, its value is unchanged and always refers to the same external object. All instances' operations on the object will affect other Example:
function Person() { } Person.prototype.name = 'Rose'; Person.prototype.age = 18; Person.prototype.lessons = ["语文","数学"]; Person.prototype.getName = function () { return this.name; }; var person1 = new Person(); person1.lessons.push("英语"); var person2 = new Person(); console.log(person1.lessons);//["语文", "数学", "英语"] console.log(person2.lessons);//["语文", "数学", "英语"]//person1修改影响了person2
Sixth: constructor + prototype method (recommended)
function Person(name,age) { this.name = name; this.age = age; } Person.prototype.getName = function () { return this.name; }; var person1 = new Person('Rose', 18); var person2 = new Person('Jack', 20); console.log(person1.name);//Rose console.log(person2.name);//Jack console.log(person1.getName === person2.getName);//true//共享原型中定义的方法
Disadvantages: Properties are defined within the constructor, and methods are defined outside the constructor, which is inconsistent with the object-oriented encapsulation idea.
Seventh method: constructor + dynamic prototype method (recommended)
Method 1:
function Person(name,age) { this.name = name; this.age = age; if (typeof Person._getName === "undefined"){ Person.prototype.getName = function () { return this.name; }; Person._getName = true; } } var person1 = new Person('Rose', 18); var person2 = new Person('Jack', 20); console.log(person1.name);//Rose console.log(person2.name);//Jack console.log(person1.getName === person2.getName);//true//共享原型中定义的方法
Method 2:
function Person(name,age) { this.name = name; this.age = age; if (typeof this.getName !== "function"){ Person.prototype.getName = function () { return this.name; }; } } var person1 = new Person('Rose', 18); var person2 = new Person('Jack', 20); console.log(person1.name);//Rose console.log(person2.name);//Jack console.log(person1.getName === person2.getName);//true//共享原型中定义的方法
Extension and deletion of object attributes
Javascript An object can dynamically extend its properties using the '.' operator. Properties can be deleted using the 'delete' keyword or by setting the property's value to 'undefined'.
function Person(name,age) { this.name = name; this.age = age; if (typeof Person._getName === "undefined"){ Person.prototype.getName = function () { return this.name; }; Person._getName = true; } } var person = new Person("Rose",18); person.job = 'Engineer';//添加属性 console.log(person.job);//Engineer delete person.job;//删除属性 console.log(person.job);//undefined//删除属性后值为undefined person.age = undefined;//删除属性 console.log(person.age);//undefined//删除属性后值为undefined
Object attribute type
Data attribute
Characteristics:
[configurable]: Indicates whether it can be deleted and redefined using the delete operator, or whether it can be modified as an accessor attribute. The default is true;
[enumberable]: Indicates whether the attribute can be returned through a for-in loop. Default true;
[writable]: Indicates whether the value of the attribute can be modified. Default true;
[value]: Contains the data value of this attribute. This value is read/written. The default is undefined; for example, the name attribute is defined in the instance object person above, and its value is 'My name'. Modifications to this value are anyway done at this location.
function Person(name,age) { this.name = name; this.age = age; if (typeof Person._getName === "undefined"){ Person.prototype.getName = function () { return this.name; }; Person._getName = true; } } var person = new Person("Rose",18); Object.defineProperty(person,"name",{configurable:false,writable:false}); person.name = "Jack"; console.log(person.name);//Rose//重新赋值无效 delete person.name; console.log(person.name);//Rose//删除无效
Note :
Once configurable is set to false, you can no longer use defineProperty to modify it to true (execution will report an error: cannot redefine property: propertyName)
function Person(name,age) { this.name = name; this.age = age; if (typeof Person._getName === "undefined"){ Person.prototype.getName = function () { return this.name; }; Person._getName = true; } } var person = new Person("Rose",18); Object.defineProperty(person,"name",{configurable:false,writable:false}); person.name = "Jack"; console.log(person.name);//Rose//重新赋值无效 delete person.name; console.log(person.name);//Rose//删除无效 Object.defineProperty(person,"name",{configurable:true,writable:true});//Cannot redefine property: name
Accessor properties
Characteristics:
[configurable]: Whether the attribute can be deleted and redefined through the delete operator;
[numberable]: Whether the property can be found through a for-in loop;
[get]: Called when reading the property, default: undefined;
[set]: When writing the property Called, default: undefined;
Accessor properties cannot be defined directly and must be defined using defineProperty() or defineProperties: as follows
function Person(name,age) { this.name = name; this._age = age; if (typeof Person._getName === "undefined"){ Person.prototype.getName = function () { return this.name; }; Person._getName = true; } } var person = new Person("Rose",18); Object.defineProperty(person,"age",{ get:function () { return this._age; }, set:function (age) { this._age = age; }}); person.age = 20; console.log(person.age);//20//person.age=20是使用set方法将20赋值给_age,person.age是使用get方法将_age的读取出来 console.log(person._age);//20
Get all properties and properties of properties
Use Object.getOwnPropertyNames(object) method to get all properties;
Use Object.getOwnPropertyDescriptor(object,property ) method can obtain the characteristics of a given attribute;
function Person(name,age) { this.name = name; this._age = age; if (typeof Person._getName === "undefined"){ Person.prototype.getName = function () { return this.name; }; Person._getName = true; } } var person = new Person("Rose",18); Object.defineProperty(person,"age",{ get:function () { return this._age; }, set:function (age) { this._age = age; }}); console.log(Object.getOwnPropertyNames(person));//["name", "_age", "age"] console.log(Object.getOwnPropertyDescriptor(person,"age"));//{enumerable: false, configurable: false, get: function, set: function}
对于数据属性,可以取得:configurable,enumberable,writable和value;
对于访问器属性,可以取得:configurable,enumberable,get和set;
继承机制实现
对象冒充
function Father(name) { this.name = name ; this.getName = function () { return this.name; } } function Son(name,age) { this._newMethod = Father; this._newMethod(name); delete this._newMethod; this.age = age; this.getAge = function () { return this.age; } } var father = new Father("Tom"); var son = new Son("Jack",18); console.log(father.getName());//Tom console.log(son.getName());//Jack//继承父类getName()方法 console.log(son.getAge());//18
多继承(利用对象冒充可以实现多继承)
function FatherA(name) { this.name = name ; this.getName = function () { return this.name; } } function FatherB(job) { this.job = job; this.getJob = function () { return this.job; } } function Son(name,job,age) { this._newMethod = FatherA; this._newMethod(name); delete this._newMethod; this._newMethod = FatherB; this._newMethod(job); delete this._newMethod; this.age = age; this.getAge = function () { return this.age; } } var fatherA = new FatherA("Tom"); var fatherB = new FatherB("Engineer"); var son = new Son("Jack","Programmer",18); console.log(fatherA.getName());//Tom console.log(fatherB.getJob());//Engineer console.log(son.getName());//Jack//继承父类FatherA的getName()方法 console.log(son.getJob());//Programmer//继承父类FatherB的getJob()方法 console.log(son.getAge());//18
call()方法
function Father(name) { this.name = name ; this.getName = function () { return this.name; } } function Son(name,job,age) { Father.call(this,name); this.age = age; this.getAge = function () { return this.age; } } var father = new Father("Tom"); var son = new Son("Jack","Programmer",18); console.log(father.getName());//Tom console.log(son.getName());//Jack//继承父类getName()方法 console.log(son.getAge());//18
多继承(利用call()方法实现多继承)
function FatherA(name) { this.name = name ; this.getName = function () { return this.name; } } function FatherB(job) { this.job = job; this.getJob = function () { return this.job; } } function Son(name,job,age) { FatherA.call(this,name); FatherB.call(this,job); this.age = age; this.getAge = function () { return this.age; } } var fatherA = new FatherA("Tom"); var fatherB = new FatherB("Engineer"); var son = new Son("Jack","Programmer",18); console.log(fatherA.getName());//Tom console.log(fatherB.getJob());//Engineer console.log(son.getName());//Jack//继承父类FatherA的getName()方法 console.log(son.getJob());//Programmer//继承父类FatherB的getJob()方法 console.log(son.getAge());//18
apply()方法
function Father(name) { this.name = name ; this.getName = function () { return this.name; } } function Son(name,job,age) { Father.apply(this,new Array(name)); this.age = age; this.getAge = function () { return this.age; } } var father = new Father("Tom"); var son = new Son("Jack","Programmer",18); console.log(father.getName());//Tom console.log(son.getName());//Jack//继承父类getName()方法 console.log(son.getAge());//18
多继承(利用apply()方法实现多继承)
function FatherA(name) { this.name = name ; this.getName = function () { return this.name; } } function FatherB(job) { this.job = job; this.getJob = function () { return this.job; } } function Son(name,job,age) { FatherA.apply(this,new Array(name)); FatherB.apply(this,new Array(job)); this.age = age; this.getAge = function () { return this.age; } } var fatherA = new FatherA("Tom"); var fatherB = new FatherB("Engineer"); var son = new Son("Jack","Programmer",18); console.log(fatherA.getName());//Tom console.log(fatherB.getJob());//Engineer console.log(son.getName());//Jack//继承父类FatherA的getName()方法 console.log(son.getJob());//Programmer//继承父类FatherB的getJob()方法 console.log(son.getAge());//18
原型链方法
function Father() { } Father.prototype.name = "Tom"; Father.prototype.getName = function () { return this.name; }; function Son() { } Son.prototype = new Father(); Son.prototype.age = 18; Son.prototype.getAge = function () { return this.age; }; var father = new Father(); var son = new Son(); console.log(father.getName());//Tom console.log(son.getName());//Tom//继承父类FatherA的getName()方法 console.log(son.getAge());//18
混合方式(call()+原型链)
function Father(name) { this.name = name; } Father.prototype.getName = function () { return this.name; }; function Son(name,age) { Father.call(this,name); this.age = age; } Son.prototype = new Father(); Son.prototype.getAge = function () { return this.age; }; var father = new Father("Tom"); var son = new Son("Jack",18); console.log(father.getName());//Tom console.log(son.getName());//Jack//继承父类Father的getName()方法 console.log(son.getAge());//18
多态机制实现
function Person(name) { this.name = name; if (typeof this.getName !== "function"){ Person.prototype.getName = function () { return this.name; } } if (typeof this.toEat !== "function"){ Person.prototype.toEat = function (animal) { console.log( this.getName() + "说去吃饭:"); animal.eat(); } } } function Animal(name) { this.name = name; if (typeof this.getName !== "function"){ Animal.prototype.getName = function () { return this.name; } } } function Cat(name) { Animal.call(this,name); if (typeof this.eat !== "function"){ Cat.prototype.eat = function () { console.log(this.getName() + "吃鱼"); } } } Cat.prototype = new Animal(); function Dog(name) { Animal.call(this,name); if (typeof this.eat !== "function"){ Dog.prototype.eat = function () { console.log(this.getName() + "啃骨头"); } } } Dog.prototype = new Animal(); var person = new Person("Tom"); person.toEat(new Cat("cat"));//Tom说去吃饭:cat吃鱼 person.toEat(new Dog("dog"));//Tom说去吃饭:dog啃骨头
The above is the detailed content of An introduction to object-oriented thinking in JavaScript. For more information, please follow other related articles on the PHP Chinese website!