以下代码为变量 car 设置值为 "Fiat" :
var car =
"Fiat";
对象也是一个变量,但对象可以包含多个值(多个变量)。
var car = {type:
"Fiat", model:
500, color:
"white"};
在以上实例中,3 个值 ("Fiat", 500, "white") 赋予变量 car。
在以上实例中,3 个变量 (type, model, color) 赋予变量 car。
![]() | JavaScript 对象是变量的容器 |
---|
prototype 属性
定义和用法
prototype 属性允许您向对象添加属性和方法
注意: Prototype 是全局属性,适用于所有的Javascript对象。
语法
object.prototype.name=value
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<script>
function
employee(name, jobtitle, born) {
this
.name = name;
this
.jobtitle = jobtitle;
this
.born = born;
}
var
fred =
new
employee(
"Fred Flintstone"
,
"Caveman"
,
1970
);
employee.prototype.salary =
null
;
fred.salary =
20000
;
document.write(fred.salary);
</script>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
function
Person(name) {
var
_this = {};
_this._name = name;
_this.sayHello =
function
() {
alert(
"P_hello"
+ _this._name);
}
return
_this;
}
function
Teacher(name) {
// body...
var
_this = Person(name);
var
superSay = _this.sayHello;
_this.sayHello =
function
(argument) {
superSay.call(_this);
alert(
"T_hello"
+ _this._name);
}
return
_this;
}
var
t = Teacher(
"aaaa"
);
//console.log(t);
t.sayHello();
|