Javascript的封裝
¶Javascript的封裝
原文連結: https://darkblack01.blogspot.com/2016/05/javascript_8.html
移植時的最後更新日期: 2016-05-04T14:39:58.316+08:00
封裝1
宣告+定義
function Female(name, age) {
this.name = name;
var age = age;
this.sayAge = function() {
if (age > 18) {
console.log(this.name + ' is 18.');
}
else
{
console.log(this.name + ' is ' + age + '.');
}
};
};
呼叫執行
var mary = new Female('Mary', 16);
mary.sayAge();
var susan = new Female('Susan', 36);
susan.sayAge();
顯示
Mary is 16.
Susan is 18.
繼承2
宣告+定義
function Female(name, age) {
this.name = name;
var age = age;
this.sayAge = function() {
if (age > 18) {
console.log(this.name + ' is 18.');
}
else
{
//sayRealAge is a function
//sayRealAge() is a function return value
console.log(this.name + ' is ' + sayRealAge() + '.');
}
};
var sayRealAge = function() {
return age;
};
};
呼叫執行
var mary = new Female('Mary', 16);
mary.sayAge();
var susan = new Female('Susan', 36);
susan.sayAge();
顯示
Mary is 16.
Susan is 18.
發表於
tags:
{ javascript }