将括号内的字符串传递给另一个字符串

Passing a string within brackets to another string

我的问题与 JS 和自定义元素有关。

我有以下代码:

let product=this.getProduct(message['productname']);

我不清楚的是 message 是上面的一个字符串...

将括号内的字符串(即['productname'])传递给另一个字符串(即message)的结果是什么?

如此处所示:

message['productname']

这个 notation/syntax 的名称是什么?

完整列表如下:

import { Component, TemplateRef, Renderer2, OnDestroy, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';


export class Product {
  public productname:string;
  public code: string;
  public cartprice: number;
  public price: number;
  public available: number;
  public qty: number;
  constructor(){

  }
}

@Component({
  selector: 'product-cart',
  templateUrl: './productcart.component.html',
  styleUrls: ['./productcart.component.css'],
  encapsulation: ViewEncapsulation.Emulated
})
export class ProductCartComponent implements OnInit, OnDestroy {

  public productlist : Product[]; 
  public totalprice=0;
  public ngOnInit(): void {
    this.productlist=[];
  }
  constructor(private renderer: Renderer2) {
  }
  @Input()
  set message(message: string) {
    this.processMessage(message);
  }
  get message(): string { return this._message; }
  _message: string;

  processMessage(message) {
    let product=this.getProduct(message['productname']);
    if(product !== undefined) {
      product.qty=product.qty + 1;
      product.cartprice=product.cartprice+message['price'];
      this.totalprice=this.totalprice+message['price'];
     } else if(message !== "" && message !== undefined) {
      product = new Product();
      product.qty=1;
      product.price=(message['price']!== undefined)?message['price']:0;
      product.productname=(message['productname'] !== undefined)?message['productname']:"";
      product.available=(message['available'] !== undefined)?message['available']:0;
      product.code=(message['code'] !== undefined)?message['code']:"";
      product.cartprice=(message['price'] !== undefined)?message['price']:0;
      this.productlist.push(product);
      this.totalprice=this.totalprice+product.price;
    }

  }

  getProduct(productname) : Product {
    let productObj=undefined;
    for(let product of this.productlist) {
      if(product.productname === productname) {
        productObj=product; 
        break;
      }
  }
  return productObj;
  }
  ngOnDestroy() {
  }
  increment(product) {
    if(product.qty >= 0 && product.qty < product.available) {
      product.qty =product.qty + 1;
      product.cartprice = product.cartprice + product.price;
      this.totalprice = this.totalprice + product.price;
      this.sendMessageToProductView(product);
    }
  }

  decrement(product) {
    if(product.qty > 0 && product.qty <= product.available) {
      product.qty =product.qty - 1;
      product.cartprice = product.cartprice - product.price;
      this.totalprice = this.totalprice - product.price;
      this.sendMessageToProductView(product);
    }
  }
  sendMessageToProductView(product) {
    const productviewele = document.querySelector('product-view');
    if(productviewele != null) {
      productviewele['message']=product;
    }
  }
}

编辑: 我可以确认消息是对象类型而不是字符串类型。 setter 参数输入错误,正如 Jonas 所暗示的那样。

message['productname'] 与 message.productname 相同,如果 message 是字符串,则它将是未定义的。如果 message 是一个对象,它将是 productname属性.

所以代码:

product.productname=(message['productname'] !== undefined)?message['productname']:"";

如果消息是对象,则似乎是获取产品名称;如果是字符串,则似乎是获取“”。