Javascript中是否有空合并运算符?
例如,在C#中,我可以这样做:
String someString = null;
var whatIWant = someString ?? "Cookies!";
Run Code Online (Sandbox Code Playgroud)
我可以为Javascript找出的最佳近似值是使用条件运算符:
var someString = null;
var whatIWant = someString ? someString : 'Cookies!';
Run Code Online (Sandbox Code Playgroud)
这有点ick嘿恕我直言.我可以做得更好吗?
javascript operators null-coalescing-operator null-coalescing
PHP将所有数组视为关联的,因此没有任何内置函数.任何人都可以推荐一种相当有效的方法来检查数组是否只包含数字键?
基本上,我希望能够区分这个:
$sequentialArray = array('apple', 'orange', 'tomato', 'carrot');
Run Code Online (Sandbox Code Playgroud)
还有这个:
$assocArray = array('fruit1' => 'apple',
'fruit2' => 'orange',
'veg1' => 'tomato',
'veg2' => 'carrot');
Run Code Online (Sandbox Code Playgroud) 我想java.util.Collection根据谓词过滤一个.
在Android SDK文档中,与@ drawable/my_image xml语法一起使用的所有示例都直接处理存储在项目的res/drawable目录中的图像.
我想知道在drawable目录中创建子目录是否明确不可行.
例如,如果我有以下目录布局:
res/drawable
-- sandwiches
-- tunaOnRye.png
-- hamAndSwiss.png
-- drinks
-- coldOne.png
-- hotTea.png
Run Code Online (Sandbox Code Playgroud)
我可以参考金枪鱼沙拉三明治的图像@ drawable/sandwiches/tunaOnRye
或者我必须将层次结构保持在drawable目录中.
在Java中,我们使用final带变量的关键字来指定其值不被更改.但我发现你可以改变类的构造函数/方法中的值.同样,如果变量是,static那么它是编译错误.
这是代码:
import java.util.ArrayList;
import java.util.List;
class Test {
private final List foo;
public Test()
{
foo = new ArrayList();
foo.add("foo"); // Modification-1
}
public static void main(String[] args)
{
Test t = new Test();
t.foo.add("bar"); // Modification-2
System.out.println("print - " + t.foo);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常,没有错误.
现在将变量更改为static:
private static final List foo;
Run Code Online (Sandbox Code Playgroud)
现在是编译错误.这final真的有用吗?
我有一个带有两个构造函数的类,一个不带参数,另一个带一个参数.
使用带有一个参数的构造函数创建对象可以按预期工作.但是,如果我使用不带参数的构造函数创建对象,我会收到错误.
例如,如果我编译此代码(使用g ++ 4.0.1)...
class Foo
{
public:
Foo() {};
Foo(int a) {};
void bar() {};
};
int main()
{
// this works...
Foo foo1(1);
foo1.bar();
// this does not...
Foo foo2();
foo2.bar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
...我收到以下错误:
nonclass.cpp: In function ‘int main(int, const char**)’:
nonclass.cpp:17: error: request for member ‘bar’ in ‘foo2’, which is of non-class type ‘Foo ()()’
Run Code Online (Sandbox Code Playgroud)
为什么这样,我如何使它工作?
我在javascript中有一个字符串,如`#box2',我只想要它的'2'.
尝试:
var thestring = $(this).attr('href');
var thenum = thestring.replace( /(^.+)(\w\d+\w)(.+$)/i,'$2');
alert(thenum);
Run Code Online (Sandbox Code Playgroud)
它仍会在警报中返回#box2,我该如何让它工作?
它需要适应末端附加的任何长度编号.
在PHP中你可以做这样惊人/可怕的事情:
$a = 1;
$b = 2;
$c = 3;
$name = 'a';
echo $$name;
// prints 1
Run Code Online (Sandbox Code Playgroud)
有没有办法用Javascript做这样的事情?
例如,如果我有一个var name = 'the name of the variable';可以获得名称变量的引用name?
我正在阅读我的C++讲师的一些讲义,他写了以下内容:
- 使用缩进//确定
- 永远不要依赖运算符优先级 - 始终使用括号//确定
- 总是使用{}块 - 即使是单行// 不行,为什么???
- 比较左侧的Const对象// OK
- 对于> = 0 //好玩法的变量使用无符号
- 删除后将指针设置为NULL - 双删除保护//不错
第三种技术对我来说并不清楚:通过在一条线中放置一条线可以获得{ ... }什么?
例如,拿这个奇怪的代码:
int j = 0;
for (int i = 0 ; i < 100 ; ++i)
{
if (i % 2 == 0)
{
j++;
}
}
Run Code Online (Sandbox Code Playgroud)
并替换为:
int j = 0;
for (int i = 0 ; i < 100 ; ++i)
if (i % 2 == 0)
j++;
Run Code Online (Sandbox Code Playgroud)
使用第一个版本有什么好处?
我试图扩展这里给出的JSON.net示例 http://james.newtonking.com/projects/json/help/CustomCreationConverter.html
我有另一个派生自基类/接口的子类
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employee : Person
{
public string Department { get; set; }
public string JobTitle { get; set; }
}
public class Artist : Person
{
public string Skill { get; set; }
}
List<Person> people = new List<Person>
{
new Employee(),
new Employee(),
new Artist(),
};
Run Code Online (Sandbox Code Playgroud)
如何将Json反序列化回List <Person>
[
{
"Department": "Department1",
"JobTitle": "JobTitle1",
"FirstName": "FirstName1", …Run Code Online (Sandbox Code Playgroud) javascript ×3
c++ ×2
java ×2
android ×1
arrays ×1
c ×1
c# ×1
coding-style ×1
collections ×1
curly-braces ×1
drawable ×1
filter ×1
final ×1
json ×1
json.net ×1
numbers ×1
operators ×1
php ×1
string ×1