(1)和(2)之间是否有任何重要的差异(语义,表现相关等)?
var obj = obj || {};
var obj = Object(obj);
上下文。第一个是我在Django的模板和我的js文件中共享变量的方式。我刚刚在Rauschmayer 2014中看到了一个部分("转换布尔,数字,字符串和对象的函数",第79页),他将Object(obj)
描述如下:
obj
如果obj
是对象,{}
如果obj
为undefined
或null
,obj
是原始值。鉴于(非常非正式)语义(特别是前两个条款),在我看来,我们可以使用(2)我们将使用的任何地方(1),但我不完全确定这会产生什么后果有
Rauschmayer,Axel(2014)Speaking JavaScript(免费在线阅读)。
答案 0 :(得分:13)
是的,因为您提到的引用已经提及,所以存在差异。当## app/config/routes.rb
Rails.application.routes.draw do
## Site's Root Route
root 'pages#home'
## Static Page Routes
get 'home' => 'pages#home'
get 'about' => 'pages#about'
get 'contact' => 'pages#contact'
get 'privacy' => 'pages#privacy'
get 'sitemap' => 'pages#sitemap'
## Administrative Routes
get 'admin/dashboard'
get 'admin/moderate_users'
get 'admin/moderate_events'
get 'admin/moderate_event_items'
get 'admin/moderate_companies'
get 'admin/moderate_locations'
get 'admin/moderate_stories'
## Customed Devise Routes
devise_for :users,
:skip => [:sessions, :registrations]
devise_scope :user do
get "login", to: "devise/sessions#new", as: :new_user_session
post "login", to: "devise/sessions#create", as: :user_session
delete "logout", to: "devise/sessions#destroy", as: :destroy_user_session
get "register", to: "devise/registrations#new", as: :new_user_registration
post "register", to: "devise/registrations#create", as: :user_registration
get "account/delete", to: "devise/registrations#cancel", as: :cancel_user_registration
get "user/profile/edit", to: "devise/registrations#edit", as: :edit_user_registration
patch "user", to: "devise/registrations#update"
put "user", to: "devise/registrations#update"
put "register", to: "devise/registrations#update"
delete "user/delete", to: "devise/registrations#destrony"
get "user/profile", to: 'devise/registrations#edit', as: :user_root
end
end
是原语时,第一个表示法将返回原语。第二个将返回一个包装的原语,它是一个对象。
如果其余代码依赖于obj
作为对象,则第二种符号更具防御性。
但话又说回来,第一种符号更常见,人们可能想知道如何将原始值分配给名为obj
的变量。