日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

原型共享数据 原型简单语法 原型中方法是可以相互访问 实例对象属性方法层层搜索

發布時間:2024/4/13 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 原型共享数据 原型简单语法 原型中方法是可以相互访问 实例对象属性方法层层搜索 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

利用原型共享數據

<!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年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的原型共享数据 原型简单语法 原型中方法是可以相互访问 实例对象属性方法层层搜索的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。