• js为Object对象动态添加属性和值 eval c.k c[k]


    const appendInfo = () => {
      const API_SECRET_KEY = 'https://github.com/dyq086/wepy-mall/tree/master/src'
      const TIMESTAMP = 'util.getCurrentTime()'
      const SIGN = 'md5.hex_md5((TIMESTAMP + API_SECRET_KEY).toLowerCase())'
      return {
        'API_SECRET_KEY': API_SECRET_KEY,
        'TIMESTAMP': TIMESTAMP,
        'SIGN': SIGN
      }
    }
    
    let data = {}
    const isSign = 2 || false
    if (isSign) {
      const a = appendInfo()
      for (let k in a) {
        console.log(k)
        console.log(a.k)
        eval('data.' + k + '= a.' + k)
      }
    }
    console.log(data)
    
    

    let a =3
    let b = {}
    while(a){
    eval('b.tag_'+(a-1)+'='+a*a)
    a--
    }

      {tag_2: 9, tag_1: 4, tag_0: 1}

    let a =3
    let b = {}
    while(a){
    eval('b.tag_'+(a-1))
    a--
    }

      {}

    c.k  添加的属性名为"k"

    c[k]                          k的值

    删除 

    this.notHere
     
    onNavbarTap(e) {
    const idx = e.currentTarget.dataset.idx
    this.nav.currentTab = idx
    switch (idx) {
    case 1:
    delete this.notHere.package
    this.notHere.PickerVal = [this.bizArea[0][1], this.bizType[0][1]]
    this.getPartnerSitesPart()
    break
    case 2:
     
     
    关于给javascript对象添加、删除、修改对象的属性 - goweb - 博客园 https://www.cnblogs.com/goweb/p/5357640.html
     
    • 利用动态特性

    复制代码
     1             function Person(){};
     2             var person = new Person();
     3             person.name = 'yy';
     4             person.gender = 'girl';
     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
     
     

    let formData = {}
    formData['key'] = ossKey
    formData['OSSAccessKeyId'] = AccessKeyId
    formData['policy'] = PolicyBase64
    formData['Signature'] = Signature
    formData['x-oss-security-token'] = STS.SecurityToken

    return {
    key: ossKey,
    OSSAccessKeyId: AccessKeyId,
    policy: PolicyBase64,
    Signature: Signature
    }

  • 相关阅读:
    随笔和文章的区别
    02. 什么是 SOA?
    GOF 23种设计模式(Patterns in Java)
    ASP.NET MVC: how to include <span> in link from Html.ActionLink?
    Head First 02.观察者模式(Observer)
    ActionResult types in MVC2
    探索推荐引擎内部的秘密,第 1 部分: 推荐引擎初探
    Head First 01.策略模式(Strategy Pattern)
    SOA实施:不能忽略商业价值
    nmon analyser——生成 AIX 性能报告的免费工具
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9595749.html
Copyright © 2020-2023  润新知