zoukankan      html  css  js  c++  java
  • JavaScript对象添加、删除、修改对象的属性

    https://www.cnblogs.com/goweb/p/5357640.html

    利用动态特性

    function Person(){};
                var person = new Person();
                person.name = 'yy';
                person.gender = 'girl';
                console.log(person.name+','+person.gender);//yy,girl
                delete person.name;//删除属性
                console.log(person.name);//undefined
    //            变式
                function Person(){};
                var person = new Person();
                person['name'] = 'yy';
                person['gender'] = 'girl';
                console.log(person.name+','+person.gender);//yy,girl
                delete person.name;//删除属性
                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
    

     

  • 相关阅读:
    CentOS7中使用yum安装Nginx的方法
    Flask&&人工智能AI --4
    Flask&&人工智能AI --3
    Flask&&人工智能AI --2
    Flask&&人工智能AI --1
    Linux--8
    Linux--7
    django 请求生命周期
    Linux--6 redis订阅发布、持久化、集群cluster、nginx入门
    Node.js Addons翻译(C/C++扩展)
  • 原文地址:https://www.cnblogs.com/qianjinyan/p/8921259.html
Copyright © 2011-2022 走看看