js深入研究之Person类案例
生活随笔
收集整理的這篇文章主要介紹了
js深入研究之Person类案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<script type="text/javascript">
/* 定義一個Person類 */
function Person(name, age) {this.name = name;this.age = age;
}
/* 添加兩個方法getName getAge */
Person.prototype = {getName: function() {return this.name;},getAge: function() {return this.age;}
}/* 實例化類 */var alice = new Person('Alice', 93);
var bill = new Person('Bill', 30);/* 修改類,添加新的方法getGreeting */Person.prototype.getGreeting = function() {return 'Hi ' + this.getName() + '!';
};alert(alice.getGreeting()); // 即可生效/* 為對象添加方法,只對對象有效 */alice.displayGreeting = function() {alert(this.getGreeting());
}alice.displayGreeting(); // Hi Alice!
bill.displayGreeting(); // 沒有信息
</script>
js的類很有意思,沒想到js還有類這種概念。jQuery或者一些插件中,經常會用到這些屬性,研究透了,才能夠看得懂,甚至自己去寫一個自己的類。
轉載于:https://www.cnblogs.com/jiqing9006/p/5048734.html
總結
以上是生活随笔為你收集整理的js深入研究之Person类案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 朝闻自习室【和平里店】能住宿吗
- 下一篇: 莫干山科技木大家觉得好不好,靠谱吗?