构造函数模式
                            
                            
                                function student(props){this.name=props.name || '匿名';//默認是匿名this.grade=props.grade || 1;}student.prototype.hello=function(){console.log('hello ' this.name);}function createStudent(props){return new student(props||{})}var xiaoming=createStudent({name:'xiaoming'});xiaoming.hello();//hello xiaoming 
 
更多專業前端知識,請上 【猿2048】www.mk2048.com
                        
                        
                        傳進一個數組?
function animal(name,age,grade){this.name=name;this.age=age;this.grade=grade;}animal.prototype.hello=function(){console.log('hello ' this.name);}var chicken=new animal('chicken',3,12);chicken.hello();//hello chicken我理解的構造函數就是用new()實例化去構造
?
看了原型繼承,發現原型繼承不了
所以就看代碼
function student(name){this.name=name||'unname';}student.prototype.hello=function(){console.log('hello' this.name)}function cat(grade){student.call(this,grade);this.grade=grade||12}var a=new cat()//a.name=unname,a.grade=12,a.hello() is not a function不可以繼承student的原型, function f(){}f.prototype=student.prototype;cat.prototype=new f()var b=new cat();//b.name=unname,b.grade=12,b.hello()='hello unname'可以繼承student的原型通過call來調用繼承,并綁定this
?也可以用class來extends
class student{constructor(name){this.name=name}hello(){console.log('hello' this.name)} } class cat extends student{constructor(name,grade){super(name);this.grade=grade;}myGrade(){console.log('my grade is' this.grade)} } var a=new cat() //a.name,a.hello(),a.grade,a.myGrade()都能用?
更多專業前端知識,請上 【猿2048】www.mk2048.com
總結
                            
                        - 上一篇: 手机端调试console.log,直接引
 - 下一篇: 打印发现function toUpper