1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- function fn1() {
- console.log('fn1',this);
- }
- fn1();
- /**
- * 构造函数
- * 调用时 使用关键字 new
- * 方法名字首字母大写
- * this指向发生改变
- */
- /**
- * 方法 写在原型下
- * 属性 写在构造函数下
- */
- function Person(name,age) {
- // this指向当前实例化的对象
- // console.log(name);
- // console.log(age);
- this.name = name;
- this.age = age;
- console.log(this,'person');
- // function
- // prototype 原型
- // constructor
- Person.prototype.eat = function() {
- console.log("爱吃桃子");
- }
- }
- // new 实例化对象
- var per = new Person("孙悟空",20);
- console.log(per.name)
- console.log(per.age)
- per.eat();
- console.log(per,'111')
- // console.log(per._photo_,'原型链') //undefined
- function Animal() {
- console.log(this,'动物')
- }
- new Animal();
- /**
- * 构造函数中自带了两个值
- * prototype 原型
- * constructor 构造函数指向对象
- */
- /**
- * 原型链
- * 访问对象时 现在对象本身寻找
- * 通过_photo_去构造函数的原型上寻找
- * 若还未找到 则去prototype上找 找到则返回 未找到则返null
- */
- </script>
- </body>
- </html>
|