首页 > 语言 > JavaScript > 正文

javascript中的一些注意事项 更新中

2024-05-06 14:26:11
字体:
来源:转载
供稿:网友
一.prototype原型对象,需要注意的原则是: 
 (1). 使用原型可以大量减少每个对象对内存的需求量,因为对象可以继承许多属性。
(2). 即使属性在对象被创建之后才被添加至原型中,对象也能够继承这些属性。
示例代码:
代码如下:
<script type="text/javascript">
function print(msg)
{
document.write(msg,'<br/>');
}
function printhr()
{
document.write('<hr/>');
}
print("prototype属性:<br/>"+
"1. 使用原型可以大量减少每个对象对内存的需求量,因为对象可以继承许多属性。<br/>"+
"2. 即使属性在对象被创建之后才被添加至原型中,对象也能够继承这些属性。");
printhr();
function User(name)
{
this.name=name;
}
var zhang = new User('老张');
zhang.favchannel ='hello';
User.prototype.favchannel='CCTV';
Object.prototype.qq ='569723660';
print(zhang.favchannel);
print(zhang.qq);
</script>

运行结果为:
prototype属性:
1. 使用原型可以大量减少每个对象对内存的需求量,因为对象可以继承许多属性。
2. 即使属性在对象被创建之后才被添加至原型中,对象也能够继承这些属性。
二.setTimeout和setInterval 两个函数都定义在window对象中。setTimeout(fun_name,time_minisec)作用是在time时间后运行fun_name函数一次;setInterval(fun_name,time_minisec)作用是每隔time_sec时间都去运行fun_name函数。
示例代码如下:
代码如下:
<html>
<head></head>
<body>
<div id="show"></div>
<input type="button" value='' id='btntime'></input>
<script type='text/javascript'>
function print(msg)
{
document.write(msg,'<br/>');
}
var changetime = function()
{
var d=new Date();
var h=d.getHours();
var m=d.getMinutes();
var sec=d.getSeconds();
var ampm=(h>=12)?'PM':'AM';
if(h>12) h-=12;
if(h==0)h=12;
if(m<10) m='0'+m;
var t=h+':'+m+':'+sec+" "+ampm;
var button = document.getElementById('btntime');
button.value = "setInterval:"+t;
}
changetime();
setInterval("changetime()",1000);
function time_setTimeOut()
{
var d=new Date();
var h=d.getHours();
var m=d.getMinutes();
var sec=d.getSeconds();
var ampm=(h>=12)?'PM':'AM';
if(h>12) h-=12;
if(h==0)h=12;
if(m<10) m='0'+m;
var t=h+':'+m+':'+sec+" "+ampm;
document.getElementById('show').innerHTML='setTimeout:'+t;
setTimeout('time_setTimeOut()',1000);
}
time_setTimeOut();
</script>
</body>
</html>

三.本地对象,内置对象和宿主对象
1.本地对象包含的内容有 Object、Function、Array、String、Boolean、Number、Date、RegExp、Error、EvalError、RangeError、ReferenceError、SyntaxError、TypeError、URIError 都是一些 ECMA-262 定义的引用类型。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选