前端学习:JS(面向对象)代码笔记
创建类和对象
创建对象方式1调用Object函数
<body> </body> <script type="text/javascript"> //新建英雄:刘备 var hero=new Object(); hero.name='刘备'; hero.blood=100; hero.weapon='双股剑'; hero.attack=function(){ return this.name+'使用'+this.weapon+'发起了攻击'; }; alert(hero.attack()); var hero2=new Object(); hero2.name='关羽'; hero2.blood=100; hero2.weapon='青龙偃月刀'; hero2.attack=function(){ return this.name+'使用'+this.weapon+'发起了攻击'; }; alert(hero2.attack()); </script>
创建对象方式2使用字面形式
<body> </body> <script type="text/javascript"> var stu1={ name:'张三', age:28, sex:'男', show: function(){ return '我的名字是:'+this.name+'\n我的性别是:'+this.sex+'\n我的年龄是:'+this.age; } }; alert( stu1.show() ); var stu2={ name:'李四', age:18, sex:'女', show: function(){ return '我的名字是:'+this.name+'\n我的性别是:'+this.sex+'\n我的年龄是:'+this.age; } }; alert( stu2.show() ); </script>
创建对象方式3使用工厂函数创建多个对象
<body> </body> <script type="text/javascript"> //形参 var createPerson=function(name,age,sex){ var person = new Object(); person.name=name; person.age=age; person.sex=sex; person.show=function(){ return '我的名字是:'+this.name+'\n我的性别是:'+this.sex+'\n我的年龄是:'+this.age; } return person; }; var p1=createPerson('张三',18,'男'); var p2=createPerson('李四',19,'男'); var p3=createPerson('王五',20,'男'); alert(p1.show()); alert(p2.show()); alert(p3.show()); </script>
创建对象方式4调用构造函数创建对象
<body> </body> <script type="text/javascript"> //定义一个Person类 function Person(name,age,sex){ //this代表的是当前对象 //当前对象的名字=需要设置的名字 this.name=name; this.age=age; this.sex=sex; this.show=function(){ return '我的名字是:'+this.name+'\n我的性别是:'+this.sex+'\n我的年龄是:'+this.age; } } //创建三个对象 var p1=new Person('张三',27,'男'); var p2=new Person('李四',28,'男'); var p3=new Person('王五',29,'男'); alert(p1.show()); alert(p2.show()); alert(p3.show()); console.log(p1.show===p2.show); console.log(p1.show===p3.show); </script>
静态成员和实例成员
静态成员和实例成员
<body> </body> <script type="text/javascript"> //静态成员 类名.成员名 /* var MyMath={ PI:3.1415, MAX:function(){ return 9999; }, MIN:function(){ return 1; } }; console.log(MyMath.PI); console.log(MyMath.MAX()); console.log(MyMath.MIN()); */ //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //实例成员 对象名.成员名 function Student(name,age,sex){ this.name=name; this.age=age; this.sex=sex; this.show=function(){ return 'name='+name+'\nage='+age+'\nsex='+sex; } } //通过Student类的构造函数创建一个具体的实例对象 var stu1=new Student('小红帽',18,'女'); var stu2=new Student('大灰狼',28,'男'); console.log('姓名:'+stu1.name ); console.log('姓名:'+stu2.show() ); </script>
原型
使用全局函数
<body> </body> <script type="text/javascript"> function Student(name,age,sex){ this.name=name; this.age=age; this.sex=sex; this.show=show; } //全局函数 var show=function(){ return 'name='+name+'\nage='+age+'\nsex='+sex; }; var stu1=new Student("DICK",22,"男"); var stu2=new Student("DICK",22,"男"); //比较的是这两个对象中show方法保存的内存地址 console.log(stu1.show===stu2.show); //true </script>
使用构造函数的原型解决方法的重复创建问题
<body> </body> <script type="text/javascript"> //得到一个结论:以后我们在定义一个类的函数的时候,不要定义在该类的构造函数中,应该 //定在该类的构造函数的原型中,这样更加高效一些. //Student类的构造函数 function Student(name,age,sex){ this.name=name; this.age=age; this.sex=sex; } //向Student类的构造函数的原型中插入show方法 Student.prototype.show=function(){ return '在原型中生成的: name='+this.name+'\nage='+this.age+'\nsex='+this.sex; }; var stu1=new Student("DICK",22,"男"); var stu2=new Student("KING",22,"男"); console.log(stu1.show()); console.log(stu2.show()); //比较的是这两个对象中show方法保存的内存地址 console.log(stu1.show===stu2.show); //true </script>
对象的原型
<body> </body> <script type="text/javascript"> //Student类的构造函数 function Student(name,age,sex){ this.name=name; this.age=age; this.sex=sex; this.eat=function(){ console.log('我在构造函数中吃东西'); } } Student.prototype.eat=function(){ console.log('我在构造函数的原型中吃东西'); } //构造方法的原型 Student.prototype //对象的原型 对象名.__proto__ var stu=new Student("KING",22,"男"); //我们发现构造方法的原型和对象的原型指向的是同一个地址 //console.log(stu.__proto__===Student.prototype); //当构造函数中和构造函数的原型中同时定义了同一个方法,这个时候会调用构造函数中的eat方法. stu.eat(); </script>
属性的查找规则
<body> </body> <script type="text/javascript"> //Student类的构造函数 function Student(name,age,sex){ this.name=name; this.age=age; this.sex=sex; } //在Student类的构造函数的原型中存入属性 Student.prototype.txt='xxx'; var stu1=new Student("KING",22,"男"); //该段代码并没有修改掉原型中txt的值,是在在stu1对象中新建了一个txt属性=abc //stu1.txt='abc'; Student.prototype.txt='ABC'; var stu2=new Student("KING",22,"男"); console.log('stu1='+stu1.txt); //abc console.log('stu2='+stu2.txt); //xxx </script>
在原型中写入多个方法属性-精简写法
<body> </body> <script type="text/javascript"> /* function Person(){ } Person.prototype.eat=function(){ console.log('eat'); } Person.prototype.run=function(){ console.log('run'); } Person.prototype.sleep=function(){ console.log('sleep'); } Person.prototype.from='地球 '; var p1=new Person(); p1.eat(); p1.run(); p1.sleep(); */ function Person(){ } Person.prototype={ from: '地球', eat: function(){ console.log('eat'); }, run: function(){ console.log('run'); }, sleep: function(){ console.log('sleep'); } }; var p1=new Person(); p1.eat(); p1.run(); p1.sleep(); alert(p1.from); </script>
原生对象的原型
<body> </body> <script type="text/javascript"> //Array类的对象 var arr=[]; console.log(Array.prototype===Array.prototype); //true // 对象的原型 构造函数的原型 console.log(arr.__proto__===Array.prototype); //true console.log(Object.prototype===Array.prototype); //false </script>
拓展原生对象中原型的方法
<body> </body> <script type="text/javascript"> var arr=[21,22,43,67,12,34,20,10]; console.log('排序前: '+arr); arr.sort(); console.log('排序后: '+arr); //求出数组中所有的偶数的和 //拓展Array类构造函数的原型中的方法: getSum() Array.prototype.getSum=function(){ var sum=0; for(var i=0;i<this.length;i++){ if(this[i]%2==0){ sum+=this[i]; } } return sum; } //错误的写法!!!!!! /* Array.prototype={ getSum:function(){ var sum=0; for(var i=0;i<this.length;i++){ if(this[i]%2==0){ sum+=this[i]; } } return sum; } }; */ console.log(arr.getSum()); </script>
案例:随机生成方块
案例:贪吃蛇