《悟透javascript》学习笔记:九、原型毕露
引言
prototype源自法语,软件界的标准翻译为“原型”,代表事物的初始形态,也含有模型和样板的意义。
prototype属性
JavaScript的所有function类型的对象都有一个prototype属性。这个prototype属性本身又是一个 object 类型的对象,因此我们也可以给这个 prototype 对象添加任意的属性和方法。既然 prototype是对象的“原型”,那么由该函数构造出来的对象应该都会具有这个“原型”的特性。事实上,在构造函数的prototype上定义的所有属性和方法,都是可以通过其构造的对象直接访问和调用的。也可以这么说,prototype提供了一群同类对象共享属性和方法的机制。运行如下代码,然后观察。
<script language="javascript">
function person(name){ //构造函数person
this.name = name;
//this创建出的对象,每一个副本都会包含其属性和函数
this.say = function(){alert("hello, i'm " + this.name);}
}
//prototype创建的函数对象,所有的副本共享同一个函数
person.prototype.show = function(){alert("hi~~, i'm " + this.name);}
var p1 = new person("p1");
var p2 = new person("p2");
alert(p1.say == p2.say); //false,没共享函数
alert(p1.show == p2.show); //true,共享函数
</script>
原型链
<script language="javascript">
function person(name){
this.name = name;
}
person.prototype.say = function(){alert("hello, i'm " + this.name);}
function employee(name, salary){
//调用person作为employee的构造函数——重点
person.call(this, name); //目的是继承person的属性
this.salary = salary;
}
//子类构造函数首先需要用上层构造函数来建立 prototype对象,实现继承的概念——重点
employee.prototype = new person(); //目的是继承person的方法,只需要其 prototype 的方法,此对象的成员没有任何意义!
employee.prototype.showMoney = function(){alert(this.name + "'s salary is " + this.salary);}
var bill = new pers
相关文档:
一、后台(.cs文件)方法:
public string GetString(string name)
{
return ("Hello " + name);
}
&n ......
//统计字符串中特定字符串的个数
function getStrCount(scrstr,armstr)
{ //scrstr 源字符串 armstr 特殊字符
var count=0;
while(scrstr.indexOf(armstr) >=1 )
{
scrstr = scrstr.replace(armstr,"")
count++;
}
re ......
history对象3个方法back()、forward()和go(),这些方法可以调用历史表中包含的文档。
back()方法装入历史表中的前一个页面,等效于浏览器中的Back按钮
forward()方法装入历史表中的后一个页面,等效于浏览器中的Forward按钮
go()方法进入历史表中的特定文档,可以取整形参数或字符串参数
go(n)n>0时,装入历史表中的 ......
开发时,需要用到的JavaScript进度条~~~
<style type="text/css">
#out {
width:200px;
height:16px !important;
border:1px solid #5858D1;
text-align:center;
position:relative;
font-size:12px;
_height:18px;
}
#out, #out * {
padding:0;
margin:0;
}
#num {
height:16px;
line-h ......
引言
在JavaScript中我们使用了一种被称为 JavaScript Object Notation(缩写JSON)的形式,翻译为中文就是“JavaScript对象表示法”。它可以简介清晰地将对象的结构描述出来,从而大大降低了我们的工作量并减少了数据量。
Json示例
<script languag ......