[JavaScript]Object(对象)学习

 

创建空对象:

var o = new Object();
o.a = "A";
o.b = "B";
o.c = "C";
alert(o.a + o.b + o.c);

 

 

ABC

对象直接量:

 

var o1 = {a:1, b:2, c:3};
alert(o1.a + o1.b + o1.c);

 

 

6

嵌套的对象:

 

var o = new Object();
o.a = "A";
o.b = "B";
o.c = "C";
o.d = {x:1, y:2, z:3};
alert(o.a + o.b + o.c + o.d.x + o.d.y + o.d.z );

 

 

ABC123

未定义的对象属性:

 

var o = new Object();
o.a = "A";
o.b = "B";
o.c = "C";
o.d = {x:1, y:2, z:3};
o.a = o.no_such_property;
delete o.b;
o.c =
null;
o.d.x =
void 0;
alert(o.a + ',' + o.b + ',' + o.c + ',' + o.d.x + ',' + o.d.y + o.d.z);

 

 

undefined,undefined,null,undefined,23

枚举对象中的属性名:

 

var o = new Object();
o.a = "A";
o.b = "B";
o.c = "C";
o.d = {x:1, y:2, z:3};
var s = "";
for (i in o)
{
s += i + ",";
}
alert(s);

 

 

a,b,c,d,

构造函数:

 

function init()
{
function Book(name, author)
{
this.name = name;
this.author = author;
}
var book1 = new Book('The Republic', 'Plato');
var book2 = new Book('A History of Western Philosophy', 'Russel');
alert("<" + book1.name + ">(" + book1.author + ")");
alert("<" + book2.name + ">(" + book2.author + ")");
}

 

 

<The Republic>(Plato)

<A History of Western Philosophy>(Russel)

将方法赋给对象:(必须分别赋给创建的每一个对象)

 

function Book(name, author)
{
this.name = name;
this.author = author;
}
function toString()
{
return "<" + this.name + ">(" + this.author + ")";
}
var book1 = new Book('The Republic', 'Plato');
book1.toString = toString;
var book2 = new Book('A History of Western Philosophy', 'Russel');
book2.toString = toString;
alert(book1.toString());
alert(book2.toString());

 

 

<The Republic>(Plato)

<A History of Western Philosophy>(Russel)

将方法的赋值放在构造函数里:

 

function Book(name, author)
{
this.name = name;
this.author = author;
this.toString = toString;
}
function toString()
{
return "<" + this.name + ">(" + this.author + ")";
}
var book1 = new Book('The Republic', 'Plato');
var book2 = new Book('A History of Western Philosophy', 'Russel');
alert(book1.toString());
alert(book2.toString );

 

 

<The Republic>(Plato)

function toString()
{
return "<" + this.name + ">(" + this.author + ")";
}

prototype原型:

 

function init()
{
function Book(title, author)
{
this.title = title;
this.author = author;
}
function toString()
{
return this.title + "," + this.author + "," + this.subject;
}
Book.prototype .subject = "Philosophy";
Book.prototype.toString = toString;
var book = new Book("Republic", "Plato");
document.write(book.toString() + "<br>");
for (i = 1;i <= 5; i++)
{
book = new Book("T" + i, "A" + i)
if (i % 2 == 0) Book.prototype.subject = "Maths";
else Book.prototype.subject = "Chemistry";
document.write(book.toString() + "<br>");
}
}


 

Republic,Plato,Philosophy
T1,A1,Chemistry
T2,A2,Maths
T3,A3,Chemistry
T4,A4,Maths
T5,A5,Chemistry

即使属性是在对象被创建以后才添加到它的原型对象中的,对象也能够继承这些属性:

 

function init()
{
function Book(title, author)
{
this.title = title;
this.author = author;
}
function toString()
{
var v = "";
for (s in this)
{
v+= s + ",";
}
return v;
}
Book.prototype.toString = toString;
var book = new Book("Republic", "Plato");
document.write(book.toString() + "<br>");
Book.prototype.subject = "Philosophy";
document.write(book.toString() + "<br>");
}


 

title,author,
subject,title,author,

关于prototype,一个奇怪的问题:

 

function Book(title, author)
{
this.title = title;
this.author = author;
}
function getFields(O)
{
var v = "";
for (s in O)
{
v+= s + "=" + O[s] + ",";
}
return v;
}
var A = new Book("Ta", "Aa");
var B = new Book("Tb", "Ab");
document.write("--------------------------<br>");
document.write(getFields(A) + "<br>");
document.write(getFields(B) + "<br>");
B.pages = 508;
B.price = "$20.50";
document.write("--------------------------<br>");
document.write(getFields(A) + "<br>");
document.write(getFields(B) + "<br>");
Book.prototype = B;
document.write("--------------------------<br>");
document.write(getFields(A) + "<br>");
document.write(getFields(B) + "<br>");
document.write("--------------------------<br>");
document.write(getFields(Book.prototype) + "<br>");
var C = new Book("Tc", "Ac");
document.write("--------------------------<br>");
document.write(getFields(C) + "<br>");


 

--------------------------
title=Ta,author=Aa,
title=Tb,author=Ab,
--------------------------
title=Ta,author=Aa,
title=Tb,author=Ab,pages=508,price=$20.50,
--------------------------
title=Ta,author=Aa,

//在“Book.prototype=B”执行以后,对象A并没有获得B中的两个新属性。奇怪。新创建的对象C却获得了。
title=Tb,author=Ab,pages=508,price=$20.50,
--------------------------
title=Tb,author=Ab,pages=508,price=$20.50,
--------------------------
price=$20.50,pages=508,title=Tc,author=Ac,

面向对象的JavaScript:

 

function Book(title, author)
{
this.title = title;
//实例变量
this.author = author;
}
Book.prototype.toString = function()//实例方法
{
var a = "Book[";
for (s in this)
{
a += s + "=" + this[s] + ";";
}
a += "]";
return a;
}
Book.toString = function(book)//静态方法
{
var a = "Book[";
for (s in book)
{
a += s + "=" + book[s] + ";";
}
a += "]";
return a;
}
var A = new Book("Ta", "Aa");
Book.KIND = "book";//静态变量
document.write(A.KIND + ";" + Book.KIND + "<br>");
document.write(A.toString() + "<br>");
document.write(Book.toString(A) + "<br>");

undefined;book
Book[title=Ta;author=Aa;]
Book[title=Ta;author=Aa;]


undefined;book
Book[title=Ta;author=Aa;]
Book[title=Ta;author=Aa;]

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值