B = function(fn) {
var ex_array = ['a', 'b'];
this.b_fn = function () {
fn.apply(what_should_be_there, ex_array);
}
}
A = function() {
this.a_fn = function (a, b) {
//do sth
}
var b = new B(fn);
}
我想要做的只是将apply
与对象fn
中的函数b
一起使用,但使用" old"上下文,我的意思是在这种情况下使用类A
答案 0 :(得分:0)
如果您想将public class FragmentVictoriaLine extends android.support.v4.app.Fragment implements {
private class Victoria {
private CharSequence station;
private CharSequence zone;
private Class<? extends Activity> activityClass;
private Class<? extends android.support.v4.app.Fragment> fragmentClass;
public Victoria(int stationResId, int zoneResId, Class<? extends Activity> activityClass, Class<? extends android.support.v4.app.Fragment> fragmentClass) {
this.fragmentClass = fragmentClass;
this.activityClass = activityClass;
this.station = getResources().getString(stationResId);
this.zone = getResources().getString(zoneResId);
}
@Override
public String toString() { return station.toString(); }
public String getzone(){ return zone.toString(); }
}
private static Victoria[] mVictoria;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
public boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_victoria_line, container, false);
if (getActivity().findViewById(R.id.detail_container) != null) {
mTwoPane = true;
}else{
mTwoPane = false;
}
// Instantiate the list of stations.
mVictoria = new Victoria[]{
new Victoria(R.string.bank, R.string.zone_1, WCBankActivity.class, FragmentWCBank.class),
new Victoria(R.string.wat, R.string.zone_1, WCWATActivity.class, FragmentWCWAT.class)
};
final ListView listView = (ListView)v.findViewById(R.id.list_victoria);
listView.setAdapter(new MyAdapter(getActivity(), mVictoria));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setTextFilterEnabled(true);
});
return v;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_victoria_line, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
android.support.v7.widget.SearchView.OnQueryTextListener textChangeListener = new android.support.v7.widget.SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
myAdapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
myAdapter.getFilter().filter(query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
return super.onCreateOptionsMenu(menu);
}
static class MyAdapter extends BaseAdapter {
static class ViewHolder {
TextView station;
TextView zone;
}
LayoutInflater inflater;
Victoria[] mVictoria;
public MyAdapter(Context contexts, Victoria[] samples) {
this.mVictoria = samples;
inflater = LayoutInflater.from(contexts);
}
@Override
public int getCount() {
return mVictoria.length;
}
@Override
public Object getItem(int position) {
return mVictoria[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_dualline, null);
viewHolder = new ViewHolder();
viewHolder.station = (TextView) convertView.findViewById(R.id.list_item_station);
viewHolder.zone = (TextView) convertView.findViewById(R.id.list_item_zone);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.station.setText(mVictoria[position].station);
viewHolder.zone.setText(mVictoria[position].getzone());
return convertView;
}
}
}
用作参数数组,以便ex_array
遍历它们,请执行此操作(jsfiddle):
apply
B = function(fn) {
var ex_array = ['a', 'b'];
this.b_fn = function() {
return fn.apply(null, ex_array);
};
}
A = function() {
this.a_fn = function (a, b) {
console.log(this, a, b);
}
var b = new B(this.a_fn.bind(this));
b.b_fn(); // => A {}, "a", "b"
}
new A();
基本上,您需要做的就是将B = function(fn) {
var ex_array = ['a', 'b'];
this.b_fn = fn.bind(null, ex_array);
}
A = function() {
this.a_fn = function (a, b) {
console.log(this, a);
}
var b = new B(this.a_fn.bind(this));
b.b_fn(); // => A {}, ['a', 'b'];
}
new A();
与其当前上下文绑定。诀窍在于this.a_fn
时使用.bind(null, /* args */)
,因此它会保留原始this.b_fn
上下文,但允许您传入另一个参数(在本例中为A
)。
答案 1 :(得分:0)
这是一个ic[j] = Form1.dt.Rows[i][5].ToString();
未预先绑定到参数数组的解决方案。我添加了一些日志记录作为演示:
a_fn