您的位置:首页 > Web前端 > JavaScript

javascript-老毕版(with语句、for语句、javascript已有对象、属性和方法)

2014-07-25 18:04 591 查看

javascript 特有的语句:

with 语句:

将对象名作为参数传到with里面,with 体中所用到的这个对象的属性不需要再写:" 对象. "

<script type="text/javascript">
function Person(name,age)
{
this.name = name;
this.age = age;
}

var p = new Person("zhangsan",25);
with(p)
{
alert(name+"..."+age);
}
</script>


for( A in B ) { } 语句:

遍历对象的属性

<script type="text/javascript">
function Student(name,age)
{
this.name = name;
this.age = age;
}

var stu = new Student("zhangsan",25);
for(s in stu)
{
alert(s);
}
</script>


既然能遍历到属性,能不能取到属性的值呢:

<script type="text/javascript">
function Student(name,age)
{
this.name = name;
this.age = age;
}

var stu = new Student("zhangsan",25);
for(s in stu)
{
alert(s + ":" + stu[s]);
}
</script>


能遍历对象,那么能遍历数组吗:

原来 a 是 arr 的脚标:

<script type="text/javascript">
var arr = [5,1,2,7];
for(a in arr)
{
alert(arr[a]);
}
</script>


javascript 已有的对象:

Object:

String:

属性

constructor 属性 | length 属性 | prototype
属性


方法:

anchor 方法 | big 方法 | blink 方法 | bold
方法 | charAt 方法 | charCodeAt 方法 | concat
方法 | fixed 方法 | fontcolor 方法 | fontsize
方法 | fromCharCode 方法 | indexOf 方法 | italics
方法 |lastIndexOf 方法 | link 方法 | match
方法 | replace 方法 | search 方法 | slice
方法 | small 方法 | split 方法 | strike
方法 | sub 方法 | substr 方法 | substring
方法 | sup 方法 | toLowerCase 方法 |toUpperCase
方法 | toString 方法 | valueOf 方法





Global:把所有全局方法集中在一个对象中。

属性:

Infinity 属性 | NaN 属性

方法:

escape 方法 | eval 方法 | isFinite
方法 | isNaN 方法 | parseFloat
方法 | parseInt 方法 | unescape
方法

toString 方法:返回对象的字符串表示。

objectname.toString([radix])




Data 对象:

方法:

getDate 方法 | getDay 方法 | getFullYear
方法 | getHours 方法 | getMilliseconds
方法 | getMinutes 方法 | getMonth
方法 | getSeconds 方法 | getTime
方法 | getTimezoneOffset 方法 | getUTCDate
方法 | getUTCDay 方法 | getUTCFullYear
方法 | getUTCHours 方法 | getUTCMilliSeconds
方法 | getUTCMinutes 方法 | getUTCMonth
方法 | getUTCSeconds 方法 | getVarDate
方法 | getYear 方法 |setDate
方法 | setFullYear 方法 | setHours
方法 | setMilliSeconds 方法 | setMinutes
方法 | setMonth 方法 | setSeconds
方法 | setTime 方法 | setUTCDate
方法 | setUTCFullYear 方法 | setUTCHours
方法 | setUTCMilliseconds 方法 | setUTCMinutes
方法 | setUTCMonth 方法 | setUTCSeconds
方法 | setYear 方法 | toGMTString
方法 | toLocaleString 方法 | toUTCString
方法 | toString 方法 |valueOf
方法 | parse 方法 | UTC
方法

<script type="text/javascript">
var d = new Date();

with(d){
var month = getMonth()+1;
month = (month>9) ? month:"0"+month;
var date = getDate();
date = (date>9) ? date:"0"+date;

var day = getDay();
var arr = new Array("日","一","二","三","四","五","六");
var week = arr[day];

var hour = getHours();
hour = (hour>9) ? hour:"0"+hour;
var minute = getMinutes();
minute = (minute>9) ? minute:"0"+minute;
var second = getSeconds();
second = (second>9) ? second:"0"+second;

document.writeln(getFullYear()+"年"+
month + "月" + date + "日 星期" + week + " " +
hour + ":" + minute + ":" + second);
}

</script>


prototype 属性 的应用:给Array、String等对象添加 自定义的新方法。



示例:我们为String类添加两个方法。

StringTool.js  :



调用 trim 方法:



调用我们为String类自定义的 trim 和 reverse 两个方法:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: