var ctr;
var lst = ["", "xml", "w1", "w2"];
var ids = [];
ctr = 0;
for (y = 0; y < lst.length; y++) {
if (lst[y].substring(0, 1) === "w") {
ids[y] = lst[y];
ctr = ctr + 1;
}
}
console.log([ids, ctr]);
Run Code Online (Sandbox Code Playgroud)
输出:[[undefined, undefined, 'w1','w2'], 2]
预期输出:[['w1','w2'],2]
我究竟做错了什么?计数器返回了我预期的数字,但为什么我的列表中有2个未定义?为什么会这样?
我刚刚在 PHP 中看到了这个语法:
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
Run Code Online (Sandbox Code Playgroud)
为什么我们在 JavaScript 中没有相同的内容?
我厌倦了这样做:
var name = obj['name'] ? obj['name'] : 'GOD';Run Code Online (Sandbox Code Playgroud)
我对这里的语法有点困惑。
window.foo = window.bar || {};
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?我只是想更好地理解 javaScript。谢谢
我毫不怀疑这是一个问题的重复,但我找不到正确的搜索词.
我想要一个速记变量声明,如果它是假的,将不会被定义.我记得它看起来像这样:
var jamie = jamie : 'or maybe not';
Run Code Online (Sandbox Code Playgroud)
如果这使得它更容易理解,这就是我实际想要实现的目标.
if($('body').hasClass('loggedin')){
var loggedin = true;
}
Run Code Online (Sandbox Code Playgroud)
记住,我不知道它是什么.
在JavaScript中,我们可以使用以下OR两个对象/属性: -
var myURL = window.URL || window.webkitURL;
Run Code Online (Sandbox Code Playgroud)
也
function doKey(e) {
var evt = e || window.event; // compliant with ie6
//Code
}
Run Code Online (Sandbox Code Playgroud)
我们可以ORC#类对象吗?
SpecificClass1 sc1 = new SpecificClass1();
SpecificClass2 sc2 = new SpecificClass2();
sc2 = null;
var temp = sc1 || sc2;
Run Code Online (Sandbox Code Playgroud)
编辑:-
不适合我使用 ??
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MultipleCast
{
class Program
{
public class BaseClass {
}
public class SpecificClass1 : BaseClass
{
public string SpecificMethod1()
{ …Run Code Online (Sandbox Code Playgroud)