构造函数
<script> function Student(name,age) { this.name=name; this.age=age; this.study= function () { console.log("我叫"+this.name+",今年"+this.age+"岁,我爱学习。"); }; } var stu=new Student("张三","22"); var stu2=new Student("李四","22"); console.log(stu instanceof Student);//true console.log(stu2 instanceof Student);//true console.log(stu.study==stu2.study);//false study存储的是函数的引用 //使用构造函数和工厂模式不同 //1.没有创建对象 new Object() //2.直接将属性和方法给了this //3.没有return //构造函数特点 //1.必须使用new 2.首字符大写 </script>
发表评论: