javascript 面向对象1
1.面向对象:
面向对象是一种对现实世界理解和抽象的方法,
是计算机编程技术发展到一定阶段后的产物。
比如描述警察:
正直公正、刚正不阿、有刀、公务员、有枪还有棍子
抓坏人、穿着警服、惩恶扬善
类 -> 归类
人类
我和它不是一类
抽象:抽出像的部分 -> 警察
函数封装 -> 类
面向对象:
把具有相同特征的代码归为一类,然后把这个类的描述细节
挂在类的原型上的一种编程方式(思想)
// let name1 = 'yellow'; // let age1 = 18; // let name2 = '小水晶'; // let age2 = 17; // let name3 = 'dudu'; // let age3 = 19; // function person(name,age){ //类 // var name = name; // var age = age; // return {name,age}; // } //类,构造(对象的)函数,工厂函数 function person(name,age){ let obj = {}; obj.name = name; obj.age = age; return obj; //返回一个对象 } let p1 = person('yellow',18); //p1就是person的实例化对象 let p2 = person('小水晶',17); let p3 = person('dudu',19); console.log(p2);
2.面向对象
/* new Array [] new Object {} new String '' new Date new 函数一元运算符,专门(只能)用来运算函数的 运算函数可以不加(),()是用来传参的 this指向这个类 默认return这个类 如果return后面是简单类型,结果还是这个类 如果return后面是复合类型,结果就是复合类型 a + b ?: */ // new Date() function Person(name,age){ // let obj = {}; this.name = name; this.age = age; this.say = function(){ alert(this.name); } // return obj; //返回一个对象 } let p1 = new Person('dudu',19); p1.say(); // console.log(typeof p1) // function fn(a){ // // console.log(this); // // return true; // return {name:'hehe'} // // return [1,2,3,4]; // } // console.log(new fn());
3.优化
/* 当一个函数创建的时候,在这个函数中就有一些属性和方法 其中有个属性叫 prototype(原型)它的值为对象 如果实例化对象没有某个属性和方法,会去构造函数的原型下去查找 */ function Person(name,age){ this.name = name; this.age = age; // this.say = function(){ // alert(this.name); // } } Person.prototype.say = function(){ alert(this.name); } Person.prototype.xx = 10; let p1 = new Person('dudu',19); let p2 = new Person('小时候可瘦了',40); // console.dir(Person); // p1.say(); // p2.say(); // console.log(p2.xx) // console.log(); // p1.say(); // p2.say(); // console.log({} == {}); // console.log(p1.say == p2.say); // let arr = []; // let arr2 = []; // Array.prototype.push = function(){ // alert('呵呵,我把你改了'); // } // arr.push(); // arr2.push(); // console.log(arr.push == arr2.push); // let obj = {"sey":"hehe"} // Object.prototype.xx = 20; // console.log(obj.xx);
4.考一考
/* 原型链:(实例化对象才有原型链) __proto__ 实例化对象与构造函数原型的桥梁 实例化对象的原型链 === 构造函数的原型 对象是没有原型的 函数是一个特殊的对象 function -> new Function */ function Person(name,age){ this.name = name; this.age = age; } Object.prototype.hehe = '你好!'; Person.prototype.say = function(){ alert(this.name); } let p = new Person; // p.hehe = 'hehe'; /* p.hehe -> Person.prototype={} {} -> Object.prototype */ console.dir(p); // let str = '123'; // let arr = []; // str.num = 10; // arr.num = 20; // Person.num = 30; // console.dir(Person.num); // console.log(p.__proto__ === Person.prototype);
5.constructor
/* constructor: 对象的constructor == 对象的构造函数 不过这个属性容易被改写 在构造函数的原型赋值一个对象的时候,一定会被修改 解决: 在这个对象下添加一个constructor属性,指向构造函数 规律: {} -> __proto__ -> Object.prototype */ function Person(name,age){ this.name = name; this.age = age; } // Person.prototype.say = function(){ // alert(this.name); // } // Person.prototype.runing = function(){ // alert('我会跑'); // } let obj = { constructor:Person, say:function(){ alert(this.name); }, runing:function(){ alert('我会跑'); } } Person.prototype = obj; let p = new Person('zwh','10'); // p.say(); console.dir(p.constructor);