JavaObject

  • Enlace amigable (slug) de la revisión: Referencia_de_JavaScript_1.5/Objetos_globales/JavaObject
  • Título de la revisión: JavaObject
  • Id de la revisión: 185459
  • Creada:
  • Creador: Mgjbot
  • ¿Es la revisión actual?
  • Comentario robot Añadido: [[en:Core JavaScript 1.5 Reference:Global Objects:JavaObject]] <<langbot>>

Contenido de la revisión

Summary

Core Object

The type of a wrapped Java object accessed from within JavaScript code.

Created by

Any Java method which returns an object type. In addition, you can explicitly construct a JavaObject using the object's Java constructor with the Packages keyword:

new Packages.JavaClass(parameterList)

JavaClass is the fully-specified name of the object's Java class.

Parameters

parameterList 
An optional list of parameters, specified by the constructor of the Java class.

Description

The JavaObject object is an instance of a Java class that is created in or passed to JavaScript. JavaObject is a wrapper for the instance; all references to the class instance are made through the JavaObject.

Any Java data brought into JavaScript is converted to JavaScript data types. When the JavaObject is passed back to Java, it is unwrapped and can be used by Java code. See the Core JavaScript 1.5 Guide for more information about data type conversions.

Properties

Inherits public data members from the Java class of which it is an instance as properties. It also inherits public data members from any superclass as properties.

Methods

Inherits public methods from the Java class of which it is an instance. The JavaObject also inherits methods from java.lang.Object and any other superclass.

Examples

Example: Instantiating a Java Object in JavaScript

The following code creates the JavaObject theString, which is an instance of the class java.lang.String:

var theString = new Packages.java.lang.String("Hello, world");

Because the String class is in the java package, you can also use the java synonym and omit the Packages keyword when you instantiate the class:

var theString = new java.lang.String("Hello, world");

Example: Accessing methods of a Java object

Because the JavaObject theString is an instance of java.lang.String, it inherits all the public methods of java.lang.String. The following example uses the startsWith method to check whether theString begins with "Hello".

var theString = new java.lang.String("Hello, world");
theString.startsWith("Hello"); // returns true 

Example: Accessing inherited methods

Because getClass is a method of Object, and java.lang.String extends Object, the String class inherits the getClass method. Consequently, getClass is also a method of the JavaObject which instantiates String in JavaScript.

var theString = new java.lang.String("Hello, world");
theString.getClass(); // returns java.lang.String

See also

JavaArray, JavaClass, JavaPackage, Packages

{{ languages( { "en": "en/Core_JavaScript_1.5_Reference/Global_Objects/JavaObject", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/JavaObject" } ) }}

Fuente de la revisión

<p>
</p>
<h3 id="Summary" name="Summary"> Summary </h3>
<p><b>Core Object</b>
</p><p>The type of a wrapped Java object accessed from within JavaScript code.
</p>
<h3 id="Created_by" name="Created_by"> Created by </h3>
<p>Any Java method which returns an object type. In addition, you can explicitly construct a <code>JavaObject</code> using the object's Java constructor with the <code>Packages</code> keyword:
</p>
<pre class="eval">new Packages.<i>JavaClass</i>(<i>parameterList</i>)
</pre>
<p><i>JavaClass</i> is the fully-specified name of the object's Java class.
</p>
<h3 id="Parameters" name="Parameters"> Parameters </h3>
<dl><dt> <code>parameterList</code> </dt><dd> An optional list of parameters, specified by the constructor of the Java class.
</dd></dl>
<h3 id="Description" name="Description"> Description </h3>
<p>The <code>JavaObject</code> object is an instance of a Java class that is created in or passed to JavaScript. <code>JavaObject</code> is a wrapper for the instance; all references to the class instance are made through the <code>JavaObject</code>.
</p><p>Any Java data brought into JavaScript is converted to JavaScript data types. When the <code>JavaObject</code> is passed back to Java, it is unwrapped and can be used by Java code. See the <a href="es/Core_JavaScript_1.5_Guide">Core JavaScript 1.5 Guide</a> for more information about data type conversions.
</p>
<h3 id="Properties" name="Properties"> Properties </h3>
<p>Inherits public data members from the Java class of which it is an instance as properties. It also inherits public data members from any superclass as properties.
</p>
<h3 id="Methods" name="Methods"> Methods </h3>
<p>Inherits public methods from the Java class of which it is an instance. The <code>JavaObject</code> also inherits methods from <code>java.lang.Object</code> and any other superclass.
</p>
<h3 id="Examples" name="Examples"> Examples </h3>
<h4 id="Example:_Instantiating_a_Java_Object_in_JavaScript" name="Example:_Instantiating_a_Java_Object_in_JavaScript"> Example: Instantiating a Java Object in JavaScript </h4>
<p>The following code creates the <code>JavaObject</code> <code>theString</code>, which is an instance of the class <code>java.lang.String</code>:
</p>
<pre class="eval">var theString = new Packages.java.lang.String("Hello, world");
</pre>
<p>Because the <code>String</code> class is in the <code>java</code> package, you can also use the java synonym and omit the <code>Packages</code> keyword when you instantiate the class:
</p>
<pre class="eval">var theString = new java.lang.String("Hello, world");
</pre>
<h4 id="Example:_Accessing_methods_of_a_Java_object" name="Example:_Accessing_methods_of_a_Java_object"> Example: Accessing methods of a Java object </h4>
<p>Because the <code>JavaObject</code> <code>theString</code> is an instance of <code>java.lang.String</code>, it inherits all the public methods of <code>java.lang.String</code>. The following example uses the <code>startsWith</code> method to check whether <code>theString</code> begins with "Hello".
</p>
<pre class="eval">var theString = new java.lang.String("Hello, world");
theString.startsWith("Hello"); // returns true 
</pre>
<h4 id="Example:_Accessing_inherited_methods" name="Example:_Accessing_inherited_methods"> Example: Accessing inherited methods </h4>
<p>Because <code>getClass</code> is a method of <code>Object</code>, and <code>java.lang.String</code> extends <code>Object</code>, the <code>String</code> class inherits the <code>getClass</code> method. Consequently, <code>getClass</code> is also a method of the <code>JavaObject</code> which instantiates <code>String</code> in JavaScript.
</p>
<pre class="eval">var theString = new java.lang.String("Hello, world");
theString.getClass(); // returns java.lang.String
</pre>
<h3 id="See_also" name="See_also"> See also </h3>
<p><a href="es/Core_JavaScript_1.5_Reference/Global_Objects/JavaArray">JavaArray</a>,
<a href="es/Core_JavaScript_1.5_Reference/Global_Objects/JavaClass">JavaClass</a>,
<a href="es/Core_JavaScript_1.5_Reference/Global_Objects/JavaPackage">JavaPackage</a>,
<a href="es/Core_JavaScript_1.5_Reference/Global_Objects/Packages">Packages</a>
</p>
<div class="noinclude">
</div>
{{ languages( { "en": "en/Core_JavaScript_1.5_Reference/Global_Objects/JavaObject", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/JavaObject" } ) }}
Revertir a esta revisión