更改javascript属性以提供修改规则

时间:2018-07-07 13:56:06

标签: javascript algorithm class

我的以下课程结构如下:

class Player {
    constructor(id) { 
       this.limits.playaces = 1;
       this.actions.playaces = 0;
    }
}

我想应用一个修改功能,使其看起来像这样

var player = new Player();
player.modifyAction("playaces","++"); //increments playaces from actions
player.modifyAction("playaces","--"); //decrements playaces from actions
player.modifyAction("playaces","+4"); //adds 4 to playaces from actions
player.modifyAction("playaces","-1"); //subtract 1 from playaces from actions

我想避免制作地图,所以我认为eval()是最好的做法,但我想知道是否可以使用词法分析器/解析器而不是使用eval并转移到新的对象范例,从而保持类定义越短越好。

能做到吗?

2 个答案:

答案 0 :(得分:0)

您是否正在寻找类似的东西?

public class MainActivity extends AppCompatActivity {

    LoginButton loginButton;
   CallbackManager callbackManager;
   Button shareButton;
   TextView fbtext;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        loginButton=(LoginButton)findViewById(R.id.login_button);
        fbtext=(TextView) findViewById(R.id.fbtext);
        shareButton = (Button) findViewById(R.id.share_post);
callbackManager=CallbackManager.Factory.create();

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
    @Override
    public void onSuccess(LoginResult loginResult) {

        fbtext.setText(
                "User ID: "
                        + loginResult.getAccessToken().getUserId()
                        + "\n" +
                        "Auth Token: "
                        + loginResult.getAccessToken().getToken()
                      );

          }

    @Override
    public void onCancel() {
        fbtext.setText("Login attempt canceled.");

    }

    @Override
    public void onError(FacebookException error) {

        fbtext.setText("Login attempt failed.");
    }
});

    shareButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isFacebookAppInstalled()) {
                Log.e("Facebook app", " is installed");
                Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.airen_heights);
                SharePhoto photo = new SharePhoto.Builder()
                        .setBitmap(image)
                        .build();
                SharePhotoContent content = new SharePhotoContent.Builder()
                        .addPhoto(photo)
                        .build();

                ShareContent shareContent = new ShareMediaContent.Builder()
                        .addMedium(photo)
                        .build();


                ShareDialog shareDialog  = new ShareDialog(MainActivity.this);
                shareDialog.show(shareContent);



            }else{
                Log.e("app is not there ","  ");
                Toast.makeText(MainActivity.this, "Please Install facebook app to post",Toast.LENGTH_LONG).show();
            }
        }
    });




    }

    public boolean isFacebookAppInstalled(){

        try{
            ApplicationInfo info = getPackageManager().
                    getApplicationInfo("com.facebook.katana", 0 );
            return true;
        } catch( PackageManager.NameNotFoundException e ){
            return false;
        }
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode,requestCode,data);

    }


}

答案 1 :(得分:0)

不完全是。我想拥有一个词法分析器/解析器(就像eval一样),但是没有eval:

function modifyAction(property,expression) {
    eval("this."+property+expression+";");
}

然后我将以以下方式称呼它:

player.modifyAction("playaces","++") //evaluating to this.playaces++;
player.modifyAction("playaces","+=1") //evaluating to this.playaces += 1;
player.modifyAction("playaces","=1") //evaluating to this.playaces = 1;
player.modifyAction("cardsToPlay","> this.cardsInHand") //evaluating to this.cardsToPlay > this.cardsInHand, if the function were to return an comparison result.

由于存在明显的安全性和性能问题,我想避免评估。我已经研究了功能,但是还没有找到答案。我当时正在考虑向对象添加一个this之类的词法分析器,但是我不确定这是否是正确的路径。