在ClojureScript中,可以将要解构的原始项目存储在单独的值中。例如,当使用地图{:a "A" :b "B" :c 3 :d 4}
调用下面的匿名函数时,密钥a
和b
将被解构,原始地图将使用m
存储在:as
中操作
((fn [{:keys [a b] :as m}]
(println "a is" a)
(println "b is" b)
(println "m is" m))
{:a "A" :b "B" :c 3 :d 4})
;; => a is A
;; b is B
;; m is {:a "A" :b "B" :c 3 :d 4}
;; nil
ECMAScript中是否有类似的操作?如,
(({ a, b }) => {
console.log(`a = ${a}`);
console.log(`b = ${b}`);
console.log(`m = ${???}`);
})({ a: "A", b: "B", c: 3, d: 4 });
或者是否有必要先将原始项目存储在变量中并稍后进行解构?
((m) => {
const { a, b } = m;
console.log(`a is ${a}`);
console.log(`b is ${b}`);
console.log(`m is ${m}`);
})({ a: "A", b: "B", c: 3, d: 4 });
;; => a is A
;; b is B
;; m is [Object object]
答案 0 :(得分:2)
如果要访问原始参数,则应将其解构为函数体中的变量:
public class MainActivity extends AppCompatActivity {
FloatingActionButton fab;
Animation show_fab;
Animation hide_fab;
private List<Movie> movieList = new ArrayList<>();
MyAdapter mAdapter;
RecyclerView recycler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
show_fab = AnimationUtils.loadAnimation(getApplication(), R.anim.fab_show);
hide_fab = AnimationUtils.loadAnimation(getApplication(), R.anim.fab_hide);
recycler = (RecyclerView)findViewById(R.id.recycler);
recycler.setLayoutManager(new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false));
recycler.setHasFixedSize(true);
mAdapter = new MyAdapter(movieList);
recycler.setAdapter(mAdapter);
prepareMovieData();
recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0 && (fab.getVisibility() == View.VISIBLE)){
//fab.hide();
fab.startAnimation(hide_fab);
}
else if (dy < 0 && (fab.getVisibility() != View.VISIBLE)){
//fab.show();
fab.startAnimation(show_fab);
}
}
});
}
private void prepareMovieData() {
Movie movie = new Movie("Mad Max: Fury Road", "Action & Adventure", "2015");
movieList.add(movie);
movie = new Movie("Inside Out", "Animation, Kids & Family", "2015");
movieList.add(movie);
movie = new Movie("Star Wars: Episode VII - The Force Awakens", "Action", "2015");
movieList.add(movie);
movie = new Movie("Shaun the Sheep", "Animation", "2015");
movieList.add(movie);
movie = new Movie("The Martian", "Science Fiction & Fantasy", "2015");
movieList.add(movie);
movie = new Movie("Mission: Impossible Rogue Nation", "Action", "2015");
movieList.add(movie);
movie = new Movie("Up", "Animation", "2009");
movieList.add(movie);
movie = new Movie("Star Trek", "Science Fiction", "2009");
movieList.add(movie);
movie = new Movie("The LEGO Movie", "Animation", "2014");
movieList.add(movie);
movie = new Movie("Iron Man", "Action & Adventure", "2008");
movieList.add(movie);
movie = new Movie("Aliens", "Science Fiction", "1986");
movieList.add(movie);
movie = new Movie("Chicken Run", "Animation", "2000");
movieList.add(movie);
movie = new Movie("Back to the Future", "Science Fiction", "1985");
movieList.add(movie);
movie = new Movie("Raiders of the Lost Ark", "Action & Adventure", "1981");
movieList.add(movie);
movie = new Movie("Goldfinger", "Action & Adventure", "1965");
movieList.add(movie);
movie = new Movie("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014");
movieList.add(movie);
mAdapter.notifyDataSetChanged();
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Movie> moviesList;
public MyAdapter(List<Movie> moviesList) {
this.moviesList = moviesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.movie_list_row, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Movie movie = moviesList.get(position);
holder.title.setText(movie.getTitle());
holder.genre.setText(movie.getGenre());
holder.year.setText(movie.getYear());
}
@Override
public int getItemCount() {
return moviesList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, year, genre;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
genre = (TextView) view.findViewById(R.id.genre);
year = (TextView) view.findViewById(R.id.year);
}
}
}
}
即使没有访问它,这种风格也是有益的,因为它允许在出现问题时调试参数,尤其是在(m) => {
const { a, b } = m;
...
}
不可用的原生箭头函数中。