2014-04-10 6 views
1

Rhino 17R4를 사용하면 Object.defineProperty() 메소드를 사용하여 javascript에서 속성을 만들 수 있습니다.순수 Rhino 개체 모델에서 "Object.defineProperty"하는 방법?

public class MyGlobalObject : org.mozilla.javascript.ScriptableObject 
{ 
    public static org.mozilla.javascript.Script ___compiledScript = null; 
    public MyGlobalObject() 
    { 
     org.mozilla.javascript.Context con = org.mozilla.javascript.Context.enter(); 
     try 
     { 
      con.initStandardObjects(this); 
      string strScript = "Object.defineProperty(this,\r\n 'onload', \r\n{ set : function(val){this.set_onload(val);},\r\n get : function(){return this.get_onload();}, enumerable: true, configurable: true});"; 

      this.defineFunctionProperties(new string[] { "set_onload", "get_onload" }, typeof(MyGlobalObject), org.mozilla.javascript.ScriptableObject.DONTENUM); 
      org.mozilla.javascript.Script sc = con.compileString(strScript, "", 1, null); 
      object result_onload = con.evaluateString(this, "this.onload == undefined;", "", 1, null); // make sure it is not defined. 
      Console.WriteLine("onload is undefined? : {0}", result_onload); 
      // Define Properties Now. 
      sc.exec(con, this); 


      con.evaluateString(this, "this.onload= function(){var t1 = 1;};", "", 1, null); 
      object onloadobjectXYZ = con.evaluateString(this, "this.onload;", "", 1, null); // get function now. 
      Console.WriteLine("Onload object : {0} is found", onloadobjectXYZ); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
     } 
     org.mozilla.javascript.Context.exit(); 
    } 
    private object __onloadFunction; 
    public object get_onload() 
    { 
     Console.WriteLine("get_onload() called!"); 
     return this.__onloadFunction; 
    } 
    //[org.mozilla.javascript.annotations.JSSetter] 
    public void set_onload(object _val) 
    { 
     Console.WriteLine("set_onload() called!"); 
     this.__onloadFunction = _val; 
    } 

    public override string getClassName() 
    { 
     return "Global"; 
    } 

} 

어떻게 FunctionObject은 (하지 '스크립트 like'strScipt를 사용하여) 순수 코뿔소 개체의 작동에 "onloadobjectXYZ"동일하다 만들 수 있습니까? 그것은 setter 및 getter에 대한 FunctionObject를 만들 수있는 것 같지만 좋은 예제를 찾을 수 없습니다. 누구든지 속성을 정의하는 방법을 알고 있습니까?

미리 감사드립니다. 자바 방법 세터/게터와

+3

?? 이 질문은별로 의미가 없습니다. – Pointy

+0

기존 Java 코드를 질문에 넣어서 getter/setter를 장비 할 원시 (native?) 객체를 알 수 있습니다. – Bergi

+0

혼란을 피하기 위해 샘플을 변경했습니다. 내가 뭘하고 싶은건 세터와 getter 'onreadystatechange'속성을 만드는 것입니다. –

답변

0

defineProperty 이것은 해결 방법으로 나를 위해 작동()

this.defineProperty("onload", null, javaonloadGetMethod, javaonloadSetMethod, ScriptableObject.PERMANENT); 

object.defineProprty 약간 다릅니다.