日期:2014-05-16 浏览次数:20513 次
var empty_object = {};
var stooge = {
"first-name":"Ouyang",
"last-name":"ping",
"yyl":"yyl"
}
alert(stooge["first-name"]); //Ouyang
alert(stooge.first-name);//NaN
alert(stooge.yyl)//yyl
alert(stooge.name);//undefinedvar middle = stooge["middle-name"] ||"none"; flight.equipment // undefined flight.equipment.model // throw "TypeError" flight.equipment && flight.equipment.model //undefined
function TestObjectA()
{
this.MethodA = function()
{
alert('TestObjectA.MethodA()');
}
}
function TestObjectB()
{
this.bb = 'ccc';
this.MethodB = function()
{
alert('TestObjectB.MethodB()');
}
}
TestObjectB.prototype = new TestObjectA(); //TestObjectB继承了 TestObjectA的方法
TestObjectB.prototype.bb = 'aaaaa'; //添加属性
var temp
var b = new TestObjectB();
for (temp in b) {
alert(temp); // MethodA bb MehtedB
}
var kk = new TestObjectB();
//删除,但原型链中的没有删除
alert(kk.bb); // ccc
delete kk.bb;
alert(kk.bb); // aaaa
alert(kk.MethodA()); //TestObjectA.MethodA()
<script language="javascript">
function RP()
{
RP.PropertyA = 1;
RP.MethodA = function()
{
alert("RP.MethodA ");
};
this.PropertyA = 100;
this.MethodA = function()
{
alert("this.MethodA");
};
}
RP.prototype.PropertyA = 10;
RP.prototype.MethodA = function()
{
alert("RP.prototype.MethodA");
};
</script> 不要着急,还没有开始做示例,只是给出了我们用来演示的一个类。RP是什么?rpwt吗?当然不是了,RP是ResearchPrototype了。好了不废话了,看示例及结果分析。 <script language="javascript">
rp = new RP();
alert(RP.PropertyA);
RP.MethodA();
alert(rp.PropertyA);
rp.MethodA();
</script>
运行结果闪亮登场:
1
RP.MethodA
100
this.MethodA
这个%$@#^$%&^...,不要着急,继续看哦! <script language="javascript">
rp = new RP();
delete RP.PropertyA;
alert(RP.PropertyA);
delete RP.MethodA;
RP.MethodA();
delete rp.PropertyA;
alert(rp.PropertyA);
delete rp.MethodA;
rp.MethodA();
</script>
运行结果再次登场:
undefined
A Runtime Error has occurred.
Do you wish to Debug?
Line: 32
Error: Object doesn't support this property or method
10
RP.prototype.MethodA