1. How JS creates objects
Literal way to create objects: the code structure is simple, suitable for the creation of a single object, the disadvantage is that mass creation of objects is inefficient, the code will be bloated
Let stu = {name:" stu ", age :22, sex:" male ", show:function(){console.log(stu.name+stu.age+this.sex); } } console.log(stu.name); stu.show();Copy the code
How factory functions create objects
function createStu(name,age,sex){ let obj = {}; obj.name = name; obj.age = age; obj.sex = sex; obj.show = function(){ console.log(obj.name+obj.age+obj.sex); } return obj; } let stu2 = createStu("lisi",18," female "); stu2.show(); console.log(stu2.sex);Copy the code
The way the constructor creates an object
function Student(){ this.name = name; this.age = age; this.sex = sex; } Student.prototype.show = function(){ console.log(this.name+this.age+this.sex); } let stu4 = new Student(" c ", 15, "F"); console.log(stu4.sex); console.log(Student.prototype === stu4.__proto__); console.log(Student.prototype); console.log(stu4.__proto__);Copy the code
2. Use a prototype approach to solve data sharing problems
First of all, I have a function here
function Student(name,age,sex){ this.name = name; this.age = age; this.sex = sex; this.show = function(){ console.log(this.name+this.age+this.sex); }}Copy the code
Solve data sharing problems by prototyping
Student.prototype.play = function(){ console.log("play games"); } Student.prototype.color = "yellow"; Let stu1 = new Student("张三", 18, "男"); Let stu2 = new Student(" 四",21," female "); stu1.play(); stu2.play(); console.log(stu1.color); console.log(stu2.color); console.log(stu1.play = stu2.play); // The paly that stu1 and stu2 call is the same function // The prototype object console.log(stu1.__proto__) from the object; // Get console.log(student.prototype) from class; // Get the console.log(stu1.__proto__. Constructor) from the object; / / get the constructor by the prototype of the class the console. The log (Student) prototype) constructor); // The object itself can get the console.log(stu1.constructor) directly;Copy the code
The Prototype attribute belongs to the constructor (class), and __proto__ belongs to the instantiated object. Prototype is a standard attribute, __proto__ is a non-standard attribute instantiated by the constructor, contains a reference to the constructor of the prototype object (proto) all instances, directly or indirectly inherit the prototype object
3. Use prototypes to manipulate built-in objects
let d = new Date(); let time = d.getFullYear() + "-" + (d.getDay() + 1) + '-' + d.getDate(); console.log(time); Date.prototype.formatTime = function () { let time = this.getFullYear() + "-" + (this.getDay() + 1) + '-' + this.getDate(); return time; } let d2 = new Date(); console.log(d2.formatTime()); String. Prototype. SayHi = function () {console.log(this + 'hello '); } let STR = 'Daniel '; str.sayHi(); Array.prototype.numberSort = function () { console.log(this); for (let i = 0; i < this.length; i++) { for (let j = 0; j < this.length; j++) { if (this[j] > this[j + 1]) { let temp = this[j]; this[j] = this[j + 1]; this[j + 1] = temp; } } } } let arr = [11, 10, 22, 34, 14, 25, 66, 87]; arr.numberSort(); console.log(arr);Copy the code