您好,登錄后才能下訂單哦!
歡迎關注博主的網絡課堂:http://edu.51cto.com/course/15019.html
<script type="text/javascript">
var obj = new Object();
//動態添加屬性
obj.name="張三";
obj.age = 15 ;
obj["addr"]="銅鑼灣";
obj.sayHello=function(){
console.log("姓名:"+this.name+",年齡"+this.age+",地址:"+this.addr);
}
obj.sayHello();
//動態刪除屬性
delete obj.addr;
obj.sayHello();
</script>
在js中,屬性可以動態添加,也可以動態刪除
json的格式的表現形式
表現形式1
<script type="text/javascript">
var obj={"name":"zhangsan","age":15,"addr":"南京"};
console.log(obj.name,obj.age,obj.addr);
console.log(typeof obj);
</script>
表現形式2
注意:value的取值可以是以下內容
對于以上的結構,那么json的變種就非常豐富
案例1:
var obj=["張三",15,true,
{
"fatherName":"張無忌",
"motherName":"無忌張",
children:["張飛","張亮","張靚穎"]
}
];
alert(typeof obj)
alert(obj[0]);
alert(obj[3]["children"])
案例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)
}
//隱藏了一個創建的對象的過程
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>
使用原型+構造函數方式來定義對象,對象之間的屬性互不干擾,各個對象間共享同一個方法
優化上面的案例
<script type="text/javascript">
function Person(name,age) {
this.name = name ;
this.age = age ;
//第一次創建對象的,通過參數給原型上增加一個共享的方法
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的參數為數組
//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) {
//只能做屬性復制
Parent.apply(this,new Array(name,age));
this.addr = addr ;
}
//拿原型中方法
Son.prototype=new Parent();
var s = new Son("zhangsan",12);
console.log(s)
<script>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。