构造函数

  1 <!DOCTYPE html>
  2 <html>
  3     <head>
  4         <meta charset="UTF-8">
  5         <title></title>
  6     </head>
  7     <body>
  8         //创建对象
  9         var a=new Object();
 10         a.username="abc"
 11         a.age="30"
 12         a.talk=function(){
 13             alert("aaa")
 14         }
 15         
 16         var b={
 17             username:"abc",
 18             age:20,
 19             talk:function(){
 20                 alert("姓名="+this.name)
 21             }
 22         }
 23         b.talk;
 24         
 25         
 26         ////////////////////////////
 27         工厂模式
 28         function createObject(username,age){
 29             var obj=new Obj();
 30             obj.username=username;
 31             obj.age=age;
 32             obj.talk=function(){
 33                 alert("姓名"+this.name )
 34             }
 35             return obj;
 36             
 37         }
 38         
 39         var persion1=createObject("abc",30)
 40         var persion2=createObject("abc",302)
 41         var persion3=createObject("abc",303)
 42         persion3.talk()
 43         
 44         //persion是引用类型,因为——
 45         var person1 = createObject('abc', 30);
 46         var person2 = person1;
 47         person2.username='2';
 48         alert(person1.username);//2
 49         alert(person2.username);//2
 50         
 51         如果——
 52                     var person1 = createObject('abc', 30);
 53                     var person2 = createObject('abc', 30);
 54         person2.username='2';
 55         alert(person1.username);//abc
 56         alert(person2.username);//2
 57         
 58         //var a = createObject('张三', 30);
 59         //var b = createObject('哈士奇', 5);
 60         //alert(b instanceof Object);//a,b都是object实例
 61         
 62         //构造函数
 63         //function Human(username, age){
 64         //    this.username=username;
 65         //    this.age=age;
 66         //    this.talk=new Function("alert(this.username+','+this.age)");
 67         //}
 68         
 69         //function Animal(username, age){
 70         //    this.username=username;
 71         //    this.age=age;
 72         //    this.talk=new Function("alert(this.username+','+this.age)");
 73         //    this.getName=function(){
 74         //        //return this.username;
 75         //        
 76         //        var that=this;        
 77         //        return function(){
 78         //            return that.username;
 79         //        }
 80         //    }
 81         //}
 82         
 83         
 84         //var name = 'The Window';
 85         //var object = {
 86         //    name:'My Object',
 87         //    getNameFunc:function(){
 88         //        //var that=this;
 89         //        return function(){
 90         //            //把this所指代的对象使用 变量that保存起来,这样即使运行环境发生了改变,that所指代的对象也不会变化
 91         //            return this.name;
 92         //        }
 93         //    }
 94         //}
 95         //alert(object.getNameFunc()());
 96         //this作为js的关键字,有特殊的含义,代表了当前对象,而当前对象是谁,是由函数执行时所处的环境来决定
 97         //当环境改变时,this所指代的对象也可能会发生变化
 98         
 99         
100         //function Student(username, age){
101         //    this.username = username;
102         //    this.age = age;
103         //}
104         //alert(Student.prototype);//是一个原型对象,原型对象是所有实例对象共享的。
105         //给原型对象增加属性
106         //Student.prototype.username='xxx';
107         //Student.prototype.isGoodman=false;
108         //Student.prototype.talk=function(){
109         //    console.log(this.username+this.age);
110         //}
111         //
112         //var stu1=new Student('比尔盖茨', 60);
113         //var stu2=new Student('孙悟空', 500);
114         //console.log(stu1.username);
115         //stu1.talk();
116         //stu1.isGoodman=true;
117         //console.log(stu1.isGoodman);
118         //console.log(stu1.__proto__.isGoodman);//__proto__原型对象的名称
119         //Student.prototype.isGoodman = true;//修改原型对象的属性,会对所有实例对象起作用,证明是共享的
120         //console.log(stu1.isGoodman);
121         //console.log(stu2.isGoodman);
122         //console.log(stu1.talk == stu2.talk);//true
123 
124         //var per1=new Person('老王');
125         //alert(per1.name);
126         //alert(Person.prototype.isPrototypeOf(per1));//检测 该原型是否属于某对象
127         //isPrototypeOf:如果对象Person存在于 对象per1的原型链中,则Person.isPrototypeOf(per1)返回true
128         
129         //delete per1.name;//指删除
130         //alert(per1.name);
131         
132         //alert(per1.hasOwnProperty('name'));//是否包含该实例属性
133         
134         //alert('name' in per1);// 是否存在该属性
135         ////////////////////////////////////////////
136         
137 //JS的继承方式
138 //1 prototype 这里所谓的继承(比较特殊的继承),他是所有实例对象共享属性,并且构造函数不能传参。 
139 //2 call 使用父类函数的call (子类对象,参数1,参数2,参数3......)  是最常用的
140 //3 apply 使用父类函数的apply (子类对象,[参数1,参数2,参数3.....])
141 
142 
143 //function Human(username, age){
144 //    this.username=username;
145 //    this.age=age;
146 //    this.introduce=function(){
147 //        alert(this.username+this.age);
148 //    }
149 //    this.sex='男';
150 //}
151 
152 
153 //function Student(username, age, score){
154     //调用父类构造函数进行传参
155     //Human.call(this, username, age);//this指代这个student,第一个参数放需要继承的单位,后面依次写父类的参数
156     //this.score=score;
157     //this.username=username;
158 //}
159 
160 //function Teacher(username, age){
161     //Human.apply(this, [username, age])
162 //}
163 
164 //Student.prototype = new Human('www');//只是拷贝Human的属性和方法
165 //Teacher.prototype = new Human();
166 
167 
168 //prorotype是什么含义?
169 //A.prototype = new B();
170 //理解prototype不应把它和继承混淆。A的prototype为B的一个实例,可以理解A将B中的方法和属性全部克隆一遍。
171 //A能使用B的方法和属性。这里强调的是克隆而不不是继承
172 
173 //prototype
174 //原型法的设计模式
175 //原型法的主要思想是,现在有1个类A,我想要创建一个类B,这个类是以A为原型的,并且能进行扩展。我们称B的原型为A。
176 
177 //js方法可以分为3类
178 // 类方法
179 // 对象方法
180 // 原型方法
181 
182 //
183 //function People(name){
184 //    this.name=name;
185 //    //对象方法
186 //    this.Introduce=function(){
187 //        alert(this.name);
188 //    }
189 //}
190 //
191 ////类方法
192 //People.Run=function(){
193 //    alert('I can run');
194 //}
195 //
196 ////原型方法
197 //People.prototype.IntroduceChinese=function(){
198 //    alert('我的名字:'+this.name);
199 //}
200 
201 
202     </body>
203 </html>
原文地址:https://www.cnblogs.com/thestudy/p/6292169.html