javascript Prototype 用法
了解一下类的概念,JavaScript 本身是一种面向对象的语言,它所涉及的元素根据其属性的不同都依附于某一个特定的类。
我们所常见的类包括:数组变量(Array)、逻辑变量(Boolean)、日期变量(Date)、结构变量(Function)、数值变量(Number)、对象变量(Object)、字符串变量(String) 等,而相关的类的方法,也是程序员经常用到的(在这里要区分一下类的注意和属性发方法),例如数组的push方法、日期的get系列方法、字符串的split方法等等;
找了一个别人的例子,先了解一下 prototype:
Number.add(num):作用,数字相加
实现方法:Number.prototype.add = function(num){return(this+num);}
试验:alert((3).add(15)) -> 显示 18
来看看javascript类的实现方法
//定义一下人
var people = function(){
this.name = 'yuxing';
};
people.prototype.sex = 'boy';
people.prototype.say = function(){
alert('i am say!');
};
var p = new people();
alert(p.name);
alert(p.sex);
p.say();
也可以这样用
//定义一下人
var people = function(){
this.name = 'yuxing';
};
people.prototype = {
sex : 'boy',
say : function(){
alert('i am say!');
}
};
var p = new people();
alert(p.name);
alert(p.sex);
p.say();
更多参考网站
http://bokee.shinylife.net/blog/article.asp?id=455
http://tech.ddvip.com/2009-05/1243588303121461.html
相关文档:
转自:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>图片跑马灯</title>
</head>
<body>
<div style="overflow:hidden; width:350px" id='div'>
<!-- 这里是第一个关键点,o ......
Dojo
一个强大的面向对象javascript框架。
主要由三大模块组成:Core、Dijit、DojoX。
Core提供 Ajax,events,packaging,CSS-based querying,animations,JSON等相关操作API。
Dijit ......
1.document.formName.item('itemname')的问题
说明:IE下可以使用document.formName.item('itemname')和document.formName.elements('elementsName');
FF下只能使用docuement.formName.elements('elementsName');
解决方法:统一使用docuement.formName.elements('elementsName');
2.集合类对象问题
说明:IE下可以使用[]和 ......
Javascript中function即为类,在function内部用this设置类的public成员变量与方法,例如:
function myclass(name){
var str = "private string"; //private field
function privatefn(){ //private method alert(str);
};
this.name = name;
......
//过滤两端的空格
function trim(str){
return str.replace(/(^\s*)|(\s*$)/g, "");
}
//过滤左边的空格
function ltrim(str){
return  ......