原型共享数据 原型简单语法 原型中方法是可以相互访问 实例对象属性方法层层搜索
生活随笔
收集整理的這篇文章主要介紹了
原型共享数据 原型简单语法 原型中方法是可以相互访问 实例对象属性方法层层搜索
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
利用原型共享數據
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// 什么樣的數據是需要寫在原型中的?// 需要共享的數據就可以寫在原型中// 原型的作用之一:數據共享// 屬性需要共享,方法也需要共享// 不需要共享的數據寫在構造函數中,需要共享的數據就可以寫在原型中// 構造函數function Student(name,age,sex){this.name = name;this.age = age;this.sex = sex;}// 所有學生的身高都是188,所有人的體重都是55// 所有學生都要每天寫500行代碼// 所有學生每天都要吃一個10斤的西瓜// 原型對象Student.prototype.height = "188";Student.prototype.weight = "55kg";Student.prototype.study = function(){console.log("學習,寫500行代碼");};Student.prototype.eat = function(){console.log("吃一個10斤的西瓜");};// 實例化對象,并初始化var stu = new Student("晨光",29,"女");console.dir(Student);console.dir(stu);// stu.eat();// stu.study();</script> </head> <body></body> </html>原型簡單的語法
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// 構造函數function Student(name,age,sex){this.name = name;this.age = age;this.sex = sex;}// 簡單的原型寫法Student.prototype = {// 手動修改構造器的指向constructor:Student,height:"188",weight:"55kg",study:function(){console.log("學習好開心啊");},eat:function(){console.log("我要吃好吃的");}};var stu = new Student("段飛",20,"男");stu.eat();stu.study();console.dir(Student);console.dir(stu);// 原型對象// Student.prototype.height = "188";// Student.prototype.weight = "55kg";// Student.prototype.study = function(){// console.log("學習,寫500行代碼");// };// Student.prototype.eat = function(){// console.log("吃一個10斤的西瓜");// };// // 實例化對象,并初始化// var stu = new Student("晨光",29,"女");// console.dir(Student);// console.dir(stu);// stu.eat();// stu.study();</script> </head> <body></body> </html>原型中的方法是可以相互訪問的
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// function Person(age){// this.age = age;// this.sayHi = function(){// console.log("hello");// // 打招呼的同時,直接調用吃的方法// // this.eat();// };// this.eat = function(){// console.log("吃東西啦");// this.sayHi();// };// }// // 實例對象的方法,是可以相互調用的// // 實例化對象,并初始化// var per = new Person(20);// // 調用方法// // per.sayHi();// per.eat();</script><script>// 原型中的方法,是可以相互訪問的function Animal(name,age){this.name = name;this.age = age;}// 原型中添加方法Animal.prototype.eat = function(){console.log("動物吃東西");this.play();};Animal.prototype.play = function(){console.log("玩球");this.sleep();};Animal.prototype.sleep = function(){console.log("睡覺了");};var dog = new Animal("小蘇",20);dog.eat();// 原型對象中的方法,可以相互調用</script> </head> <body></body> </html>實例對象使用屬性和方法層層搜索
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>function Person(age,sex){// 年齡this.age = age;// 性別this.sex = sex;this.eat = function(){console.log("構造函數中的吃");};}Person.prototype.sex = "女";Person.prototype.eat = function(){console.log("原型對象中的吃");};var per = new Person(20,"男");// 男console.log(per.sex);per.eat();console.dir(per);/*** 實例對象使用的屬性或者方法,先在實例中查找,找到了則直接使用* 找不到則去實例對象的__proto__指向的原型對象prototype中找,找到了則使用* 找不到則報錯*/</script> </head> <body></body> </html>?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的原型共享数据 原型简单语法 原型中方法是可以相互访问 实例对象属性方法层层搜索的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 总结三者之间的关系
- 下一篇: 为内置对象添加原型方法 把局部变量编程全