JS控制报错:Uncaught SyntaxError: Invalid shorthand property initializer 的解决方法: 今天复制了一串 js 代码,运行发现页面无法正常展示 ,控制台报错:Uncaught SyntaxError: Invalid shorthand property initializer 。
//Longhand if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') { //logic } //Shorthand if // Longhand let test: boolean; if (x > 100) { test = true; } else { test = false; } // Shorthand //Longhand let test1; let test2 = 1; //Shorthand let test1, test2 = 1; 4.Null, Undefined,空检查 当我们创建新的变量时 //Longhand let test1, test2, test3; test1 = 1; test2 = 2; test3 = 3; //Shorthand let [test1, test2, // Longhand test1 = test1 + 1; test2 = test2 - 1; test3 = test3 * 20; // Shorthand test1++; test2--;
//Longhand if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') { //logic } //Shorthand if // Longhand let test: boolean; if (x > 100) { test = true; } else { test = false; } // Shorthand == '') { let test2 = test1; } // Shorthand let test2 = test1 || ''; 5.null值检查和分配默认值 let test1 = //Longhand let test1, test2, test3; test1 = 1; test2 = 2; test3 = 3; //Shorthand let [test1, test2 // Longhand test1 = test1 + 1; test2 = test2 - 1; test3 = test3 * 20; // Shorthand test1++; test2--;
//longhand if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') { //logic } //shorthand if // Longhand let test: boolean; if (x > 100) { test = true; } else { test = false; } // Shorthand //shorthand const welcome = `Hi ${test1} ${test2}`; 21. 跨行字符串 当我们在代码中处理跨行字符串时,可以这样做。 双重按位操作 // Longhand Math.floor(1.9) === 1 // true // Shorthand ~~1.9 === 1 // true 31. 获取字符串的字符 let str = 'abc'; //Longhand str.charAt(2); // c //Shorthand str[2]; // c 34.
声明变量 //Longhand let x; let y = 20; //Shorthand let x, y = 20; 2. 给多个变量赋值 我们可以使用数组解构来在一行中给多个变量赋值。 //Longhand let a, b, c; a = 5; b = 8; c = 12; //Shorthand let [a, b, c] = [5, 8, 12]; 3. let x = 'Hello', y = 55; //Longhand const temp = x; x = y; y = temp; //Shorthand [x, y] = [y, x]; 7. //Longhand const power = Math.pow(4, 3); // 64 // Shorthand const power = 4**3; // 64 15. // Shorthand const arr = [2, 8, 15, 4]; Math.max(...arr); // 15 Math.min(...arr); // 2 17.
//longhand if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') { //logic } //shorthand if // Longhand let test: boolean; if (x > 100) { test = true; } else { test = false; } // Shorthand == '') { let test2 = test1; } // Shorthand let test2 = test1 || ''; 5、空值检查和分配默认值 let test1 = null //Longhand let test1, test2, test3; test1 = 1; test2 = 2; test3 = 3; //Shorthand let [test1, test2, // Longhand test1 = test1 + 1; test2 = test2 - 1; test3 = test3 * 20; // Shorthand test1++; test2--;
//Longhand if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') { //logic } //Shorthand if // Longhand let test: boolean; if (x > 100) { test = true; } else { test = false; } // Shorthand == '') { let test2 = test1; } // Shorthand let test2 = test1 || ''; 5.null 值检查和分配默认值 let test1 //Longhand let test1, test2, test3; test1 = 1; test2 = 2; test3 = 3; //Shorthand let [test1, test2, // Longhand test1 = test1 + 1; test2 = test2 - 1; test3 = test3 * 20; // Shorthand test1++; test2--;
Shorthand: ? 你还可以像下面这样嵌套if语句: ? Shorthand: ? 如果存在 这可能是微不足道的,但值得提及。做“如果检查”时,赋值操作符有时可以省略。 Longhand: ? Shorthand: ? Shorthand: ? ? Shorthand: ? 默认参数值 你可以使用if语句来定义函数参数的默认值。在ES6中,可以在函数声明中定义默认值。 Longhand: ? Shorthand: ? Shorthand: ?
//Longhand let data1; let data2= 1; //Shorthand let data1, data2= 1; 4、检查非空值 如果我们想检查变量不为空怎么办? == '') { let data2 = data1; } // Shorthand let data2 = data1 || ''; 5、分配默认值 let data1 = null, // Longhand data1 = data1 + 1; data2 = data2 - 1; data3 = data3 * 30; // Shorthand data1++; data2--; == null) // Shorthand // if (test1) 注意:如果 test1 有任何值,它将落入 if 循环之后的逻辑中,该运算符主要用于 null 或 undefined 检查。 //Longhand if (test1) { callMethod(); } //Shorthand test1 && callMethod(); 12、返回简写 这将有助于避免使用大量代码,
ageGroup; // LONG FORM if (age > 18) { ageGroup = "An adult"; } else { ageGroup = "A child"; } // SHORTHAND FORM if(maybeSomething){ console.log(maybeSomething) } else { console.log("Nothing found") } //SHORTHAND 这可以应用于交换两个变量而无需第三个: let x = 1; let y = 2; // LONGER FORM let temp = x; x = y; y = temp; // SHORTHAND nums1 = [1, 2, 3]; const nums2 = [4, 5, 6]; // LONG FORM let newArray = nums1.concat(nums2); // SHORTHAND ; } // LONGER FORM if(isReady){ doSomething(); } // SHORTHAND isReady && doSomething(); 10、类固醇的字符串
ageGroup; // LONG FORM if (age > 18) { ageGroup = "An adult"; } else { ageGroup = "A child"; } // SHORTHAND FORM if(maybeSomething){ console.log(maybeSomething) } else { console.log("Nothing found") } //SHORTHAND ; 也可以使用此语法代替将值推送到数组: let numbers = [1, 2, 3]; // LONGER FORM numbers.push(4); numbers.push(5); // SHORTHAND ; } // LONGER FORM if(isReady){ doSomething(); } // SHORTHAND isReady && doSomething(); 9、 类固醇的字符串 case 2: someOtherFunction(); break; case 3: yetAnotherFunction(); break; } // SHORTHAND
//longhand if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') { //logic } //shorthand if // Longhand let test: boolean; if (x > 100) { test = true; } else { test = false; } // Shorthand == '') { let test2 = test1; } // Shorthand let test2 = test1 || ''; 5、空值检查和分配默认值 let test1 = null //Longhand let test1, test2, test3; test1 = 1; test2 = 2; test3 = 3; //Shorthand let [test1, test2, // Longhand test1 = test1 + 1; test2 = test2 - 1; test3 = test3 * 20; // Shorthand test1++; test2--;
; let group; // LONG VERSION if (age >= 18) { group = "Adult"; } else { group = "Child"; } // SHORTHAND LONGER EXPRESSION if(someValue){ console.log(someValue) } else { console.log("Nothing found") } //SHORTHAND 例如: let numbers = [1, 2, 3]; // LONGER FORM numbers.push(4); numbers.push(5); // SHORTHAND numbers 例如: let num1, num2; // LONGER FORM num1 = 10; num2 = 100; // SHORTHAND [num1, num2] = [10, 100]; 这也适用于对象 例如: let x = 1; let y = 2; // LONGER FORM let temp = x; x = y; y = temp; // SHORTHAND [x, y] = [y, x
Golang: 14 Shorthand Tricks You Might Not Know! // Shorthand message := "Hello, Golang!" 声明和初始化多个变量 // Long form var a, b, c int a = 1 b = 2 c = 3 // Shorthand a, b, c := 1, 2 , 3 交换变量 a, b := 1, 2 // Long form temp := a a = b b = temp // Shorthand a, b = b, = nil { // Handle the error } // Shorthand if result, err := someFunction(); err !
Longhand: const x = 20;let answer;if (x > 10) { answer = 'is greater'; } else { answer = 'is lesser'; } Shorthand == '') { let variable2 = variable1; } Shorthand: const variable2 = variable1 || 'new'; 不要相信我,请先相信自己的测试 Shorthand: let a;if ( !a ) {// do something...} ; } return bar; } Shorthand: mandatory = () => { throw new Error('Missing parameter!') Longhand: Math.floor(4.9) === 4 //true Shorthand: ~~4.9 === 4 //true
== undefined) { finalStr = 'default string' } else { finalStr = str } // Shorthand let str = '' let == undefined) { actualNum = num } else { actualNum = 0 } // Shorthand let num = null let actualNum 例如: // Shorthand const name = 'Iby' const hobby = 'to read' const fullStr = `${name} loves ${hobby}. 请参阅下面的对象属性分配简写示例: // Longhand const obj = { x: 1, y: 2, z: 3 } // Shorthand const x = 8 const } // Shorthand console.log(realNum + (~realNumIndex ? ' exists!' : ' does not exist!')
原生支持更丰富的功能:支持 shorthand、deprecated、hidden 等高级功能。 二、实践 1. 标记为即将废弃的,请用户使用 des-detail 的 shorthand 参数 flag.CommandLine.MarkShorthandDeprecated("badflag", "please - 表示 shorthand,-- 表示完整的选项名称。 除了最后一个 shorthand,其它的 shorthand 都必须是布尔类型的参数或者是具有默认值的参数。 通过 MarkDeprecated 和 MarkShorthandDeprecated 方法可以分别把参数及其 shorthand 标记为废弃: // 把 badflag 参数标记为即将废弃的,请用户使用 标记为即将废弃的,请用户使用 des-detail 的 shorthand 参数 flag.CommandLine.MarkShorthandDeprecated("badflag", "please
flag:"name" usage:"student name" persistent:"true"` Age int64 `flag:"age" usage:"student age" shorthand int64 时间: timeDuration, *time.Duration flag 与 cobra 定义一致 func (f *FlagSet) Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string) { f.VarP(newUint64Value(value, p), name, shorthand, usage) } 3 " usage:"student name" persistent:"true"` AgePtr *int64 `flag:"ageptr" usage:"student age" shorthand 连接, --config.password shorthand:"c" : 参数简写 -c, 简写没有潜逃 usage:"comment balalal": 参数说明 persistent : 全局 4
flag 与 cobra 定义一致 func (f *FlagSet) Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string) { f.VarP(newUint64Value(value, p), name, shorthand, usage) } flag 设置 type student struct :"name" usage:"student name" persistent:"true"` Age int64 `flag:"age" usage:"student age" shorthand 连接, --config.password shorthand:"c" : 参数简写 -c, 简写没有潜逃 usage:"comment balalal": 参数说明 persistent : 全局 默认值设置 :"name" usage:"student name" persistent:"true"` Age int64 `flag:"age" usage:"student age" shorthand
int64时间: timeDuration, *time.Durationflag 与 cobra 定义一致func (f *FlagSet) Uint64VarP(p *uint64, name, shorthand string, value uint64, usage string) { f.VarP(newUint64Value(value, p), name, shorthand, usage)}flag flag:"name" usage:"student name" persistent:"true"` Age int64 `flag:"age" usage:"student age" shorthand nameptr" usage:"student name" persistent:"true"` AgePtr *int64 `flag:"ageptr" usage:"student age" shorthand flag:"name" usage:"student name" persistent:"true"` Age int64 `flag:"age" usage:"student age" shorthand