歡迎關(guān)注博主的網(wǎng)絡(luò)課堂:http://edu.51cto.com/course/15019.html
創(chuàng)新互聯(lián)主要從事成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)漳平,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
<script type="text/javascript">
var obj = new Object();
//動(dòng)態(tài)添加屬性
obj.name="張三";
obj.age = 15 ;
obj["addr"]="銅鑼灣";
obj.sayHello=function(){
console.log("姓名:"+this.name+",年齡"+this.age+",地址:"+this.addr);
}
obj.sayHello();
//動(dòng)態(tài)刪除屬性
delete obj.addr;
obj.sayHello();
</script>
在js中,屬性可以動(dòng)態(tài)添加,也可以動(dòng)態(tài)刪除
json的格式的表現(xiàn)形式
表現(xiàn)形式1
表現(xiàn)形式2
案例2:
var obj3= {
"name":"zhangsan",
"age":15,
children:[
{
name:"張一",
age:1
},
{
name:"lisi",
age:10
},
{
name:"wangwu",
age:12
}
],
sayInfo:function(){
console.log(this.name,this.age);
}
};
obj3.sayInfo();
for(var i = 0 ;i<obj3.children.length;i++) {
console.log(obj3.children[i].name,obj3.children[i].age)
}
function createObject(name,age){
var obj = new Object();
obj.name = name ;
obj.age = age
obj.sayHello=sayHello;
return obj ;
}
var sayHello=function(){
console.log(this.name,this.age);
}
var obj1 = createObject("張三",12);
obj1.sayHello();
var obj2 = createObject("李四",20);
obj2.sayHello();
<script type="text/javascript">
function Person(name,age){
this.name = name ;
this.age =age ;
this.sayHello= sayHello;
}
function sayHello(){
console.log(this.name,this.age)
}
//隱藏了一個(gè)創(chuàng)建的對(duì)象的過程
var p1 =new Person("張三",12);
var p2 = new Person("李四",15);
p1.sayHello();
p2.sayHello();
</script>
<script type="text/javascript">
function Person(name,age){
this.name = name ;
this.age = age ;
}
Person.prototype.sayHello = function(){
console.log(this.name,this.age);
}
var p1 = new Person("張三",15);
var p2 = new Person("lisi",16);
p1.sayHello();
p2.sayHello();
</script>
使用原型+構(gòu)造函數(shù)方式來定義對(duì)象,對(duì)象之間的屬性互不干擾,各個(gè)對(duì)象間共享同一個(gè)方法
優(yōu)化上面的案例
<script type="text/javascript">
function Person(name,age) {
this.name = name ;
this.age = age ;
//第一次創(chuàng)建對(duì)象的,通過參數(shù)給原型上增加一個(gè)共享的方法
if(typeof Person.prototype.flag==="undefined") {
alert("invoked...");
Person.prototype.sayInfo = function(){
console.log(this.name,this.age)
};
Person.prototype.flag = true ;
}
}
var p1 = new Person("zhangsan",12);
var p2 = new Person("lisi",15);
p1.sayInfo();
p2.sayInfo()
</script>
function Parent(name,age) {
this.name= name ;
this.age = age ;
this.sayInfo = function(){
console.log(this.name,this.age) ;
}
}
function Son(name,age,addr) {
this.method=Parent;
this.method(name,age);
this.addr = addr ;
//將自定義的method方法刪除掉
delete this.method;
this.sayInfo=function(){
console.log(this.name,this.age,this.addr) ;
}
}
function Parent(name,age) {
this.name = name ;
this.age = age ;
this.sayInfo=function(){
console.log(this.name,this.age);
};
}
function Son(name,age,addr) {
//call接受的離散的值,apply的參數(shù)為數(shù)組
//Parent.call(this,name,age);
Parent.apply(this,new Array(name,age));
this.addr = addr ;
this.sayInfo=function(){
console.log(this.name,this.age,this.addr);
}
}
var s = new Son("張三",15,"北京");
console.log(s)
s.sayInfo();
<script type="text/javascript">
//apply|call+原型混合方式
function Parent(name,age) {
this.name = name ;
this.age = age ;
}
Parent.prototype.sayInfo=function(){
console.log(this.name,this.age);
}
function Son(name,age,addr) {
//只能做屬性復(fù)制
Parent.apply(this,new Array(name,age));
this.addr = addr ;
}
//拿原型中方法
Son.prototype=new Parent();
var s = new Son("zhangsan",12);
console.log(s)
<script>
當(dāng)前標(biāo)題:JavaScript面向?qū)ο?/a>
轉(zhuǎn)載來于:http://jinyejixie.com/article34/ggeope.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、自適應(yīng)網(wǎng)站、電子商務(wù)、面包屑導(dǎo)航、網(wǎng)站制作、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)