Javascript 将括号内的字符串传递给另一个字符串
我的问题与JS和自定义元素有关 我有以下代码:Javascript 将括号内的字符串传递给另一个字符串,javascript,angular,typescript,custom-element,Javascript,Angular,Typescript,Custom Element,我的问题与JS和自定义元素有关 我有以下代码: let product=this.getProduct(message['productname']); 我不清楚的是,消息是上面的一个字符串 将括号内的字符串(即['productname'])传递给另一个字符串(即消息)的结果是什么 如图所示: 消息['productname'] 这个符号/语法的名称是什么 完整列表可在下面找到: import { Component, TemplateRef, Renderer2, OnDestroy, O
let product=this.getProduct(message['productname']);
我不清楚的是,消息
是上面的一个字符串
将括号内的字符串(即['productname']
)传递给另一个字符串(即消息
)的结果是什么
如图所示:
消息['productname']
这个符号/语法的名称是什么
完整列表可在下面找到:
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;
}
}
}
从'@angular/core'导入{Component、TemplateRef、renderr2、ondestory、OnInit、viewenclosuration、Input};
从“ngx引导/模式”导入{BsModalRef,BsModalService};
出口类产品{
公共产品名称:字符串;
公共代码:字符串;
公共价格:数字;
公开价格:数字;
公众可得:数量;
公共数量:数量;
构造函数(){
}
}
@组成部分({
选择器:“产品购物车”,
templateUrl:“./productcart.component.html”,
样式URL:['./productcart.component.css'],
封装:视图封装。仿真
})
导出类ProductCartComponent实现OnInit、OnDestroy{
公共产品清单:产品[];
公共总价=0;
public ngOnInit():void{
this.productlist=[];
}
构造函数(专用渲染器:渲染器2){
}
@输入()
设置消息(消息:字符串){
this.processMessage(message);
}
get message():字符串{返回此。_message;}
_消息:字符串;
processMessage(消息){
让product=this.getProduct(消息['productname']);
如果(产品!==未定义){
产品数量=产品数量+1;
product.cartprice=product.cartprice+消息['price'];
this.totalprice=this.totalprice+消息['price'];
}else if(消息!==“”&&message!==未定义){
产品=新产品();
产品数量=1;
product.price=(消息['price']!==未定义)?消息['price']:0;
product.productname=(消息['productname']!==未定义)?消息['productname']:“”;
product.available=(消息['available']!==未定义)?消息['available']:0;
product.code=(消息['code']!==未定义)?消息['code']:“”;
product.cartprice=(消息['price']!==未定义)?消息['price']:0;
this.productlist.push(产品);
this.totalprice=this.totalprice+product.price;
}
}
getProduct(productname):产品{
设productObj=未定义;
for(让此.productlist的产品){
如果(product.productname==productname){
productObj=产品;
打破
}
}
返回产品OBJ;
}
恩贡德斯特罗(){
}
增量(产品){
如果(product.qty>=0&&product.qty 如果(product.qty>0和&product.qymessage['productname']
与message.productname相同,如果message是字符串,则未定义message.productname。如果message是对象,则它将是productname
属性
因此,代码:
product.productname=(message['productname'] !== undefined)?message['productname']:"";
如果消息是对象,则似乎要获取productname;如果消息是字符串,则为“”。如果message
是字符串,则message['productname']
查找非对象的属性无效。该语法为对象属性syntax@charlietfl不,它是未定义的。一切都是js中的一个对象。感谢您的输入。这是指向github上完整源代码的链接:@charlietfl那么如果消息不是字符串,它是什么?@balteo可能代码的作者只是非常不经验把这部分搞糟了。至少代码看起来非常感谢你的输入。我只是不明白为什么setter被键入字符串…我正在与作者联系,以了解为什么…我在猜测,但可能他想将JSON字符串解析为对象,而忘了添加JSON.parse?