删除主要是delet方法;

 1             function Person(){};
 2             var person = new Person();
 3             person.name = 'yy';
 4             person.gender = 'girl';4df
 5             console.log(person.name+','+person.gender);//yy,girl
 6             delete person.name;//删除属性
 7             console.log(person.name);//undefined
 8 //            变式
 9             function Person(){};
10             var person = new Person();
11             person['name'] = 'yy';
12             person['gender'] = 'girl';
13             console.log(person.name+','+person.gender);//yy,girl
14             delete person.name;//删除属性
15             console.log(person.name);//undefined                    
复制代码

 

  • 构造函数中this

复制代码
var Person = function (){
                this.name = 'yy';
                this.gender = 'girl'
            };
            var person = new Person();
            console.log(person.name+','+person.gender);//yy,girl
复制代码

 

  • 原型中添加属性

复制代码
var Person = function (){    
            };
            Person.prototype.name = 'yy';
            Person.prototype.gender = 'girl';
            var person = new Person();
            console.log(person.name+','+person.gender);//yy,girl
            console.log(person['name']+','+person['gender']);//yy,girl
复制代码
  • 字面量添加属性

复制代码
var person = {    
            };
            person.name = 'yy';
            person.gender = 'girl';
            console.log(person.name+','+person.gender);//yy,girl
            console.log(person['name']+','+person['gender']);//yy,girl
//            另一种写法
            var person = {
                name: 'yy',
                gender: 'girl'
            };
            console.log(person.name+','+person.gender);//yy,girl
            console.log(person['name']+','+person['gender']);//yy,girl
复制代码
  • 封装extend方法

复制代码
var p1 = {};
            var p2 = {name:'yy',gender:'girl'};
            p1.extend = function(obj){
                for(var k in obj){
                    this[k] = obj[k];
                }
            }
            p1.extend(p2);
            console.log(p1.name+','+p1.gender);//yy,girl
            console.log(p1['name']+','+p1['gender']);//yy,girl
posted on 2017-10-25 21:34  IT猩  阅读(256)  评论(0编辑  收藏  举报