html += '<td>'+(sd.someKey.s != "undefined") ? sd.someKey.s.a: '-'+'</td>'
我以为我正在正确处理异常处理,但我收到Cannot read property 'a' of undefined
的错误?
答案 0 :(得分:0)
您想查看sd.someKey.s !== undefined
。您也可以查看typeof sd.someKey.s !== "undefined"
。在大多数情况下,两者都会达到相同的结果,但有一些边缘情况,后者更安全。我个人认为前者更具可读性。
html += '<td>'+(sd.someKey.s !== undefined) ? sd.someKey.s.a: '-'+'</td>'
此外,通常最好使用!==
表示法而不是!=
。
Which equals operator (== vs ===) should be used in JavaScript comparisons?
答案 1 :(得分:0)
尝试使用44.6
Object.hasOwnProperty()
答案 2 :(得分:0)
正如@DoightedD0D在他的一条评论中提到的那样,您应该使用public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
...
public void onCreate(Bundle savedInstanceState) {
...
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
}
来确定对象是否未定义。
我为你做了一些小提琴 -
https://jsfiddle.net/2oud68gL/
typeof