Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:22:14+00:00 2026-05-29T15:22:14+00:00

I’d like an example of how to access and call Javascript object properties and

  • 0

I’d like an example of how to access and call Javascript object properties and methods from C++ using the v8 engine. The documentation shows how to access C++ objects and functions through javascript but not the other way around.

Here’s a simple object constructor and instance in JS:

function MyObj()
{
    this.myArray = [];
    this.myDouble = 0;
    this.myFunction = function(arg1,arg2) 
        {   return (myDouble + arg1 + arg2);   }
}

var globalObject = new myObj();

How would I access globalObject’s properties and methods? Also a somewhat related question — how can I populate the array (globalObject.myArray) from C++?

Regards,

Pris

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:22:15+00:00Added an answer on May 29, 2026 at 3:22 pm

    I haven’t tested the examples below.

    But I believe it gives an example of what you want.

    #include <v8.h> 
    using namespace v8;
    int main(int argc, char* argv[]) {
      // Create a handle scope
      HandleScope handle_scope;
      // Create a new context. 
      Handle<Context> context = Context::New();
      // Enter the created context for compiling and 
      // running the script.
      Context::Scope context_scope(context);
      // Create a new script  
      const char* script = "function MyObj() { this.myArray = []; this.myDouble = 0; this.myFunction = function(arg1,arg2) {   return (myDouble + arg1 + arg2);  } } var globalObject = new myObj();"
      // Create a string containing the JavaScript source code. 
      Handle<String> source = String::New("script");
      // Compile the source code. 
      Handle<Script> script = Script::Compile(source);
      // Running the script
      // Run the script to get the result. 
      Handle<Value> scriptresult = script->Run();
      // Convert the result to an ASCII string and print it. 
      String::AsciiValue ascii(scriptresult);
      printf("%s\n", *ascii);
    
      // Get the object
      Handle<Object> object = Local::Cast(context->Global()->Get(String::New("globalObject")));
      // Get the Properties 
      Handle<Value> arrayproperty = Handle::Cast(object->Get(String::New("myArray")));
      Handle<Value> doubleproperty = Handle::Cast(object->Get(String::New("myDouble")));
      String::AsciiValue ascii2(arrayproperty);
      String::AsciiValue ascii3(doubleproperty);
      printf("%s\n", *ascii2);
      printf("%s\n", *ascii3);
      // Call the function
      Handle fun_to_call = Handle::Cast(object->Get(String::New("myFunction"))); 
      int argcount = 0;
      Handle argarray[] = NULL;
    
      Handle functionresult = fun_to_call->Call(object, argcount, argarray); 
     // argcount and argarray are your standard arguments to a function
    
      return 0;
    
    
    }
    

    As for how to modify the array I believe it would be using

    // Get the object
    Handle<Object> object = Local::Cast(context->Global()->Get(String::New("globalObject")))1;
    
    //Initialise array
    int num[4] = {1,2,3,4};
    v8::Local<v8::Array> arguments = v8::Array::New(num); 
    for (int i = 0; i < args; i++) { 
      arguments.Set(v8::Number::New(i), v8::String::New(args[i])); 
    } 
    
    // Set Array
    object->Set(v8::String::New("myArray"), arguments); 
    

    References

    CodeProject Using V8

    Connecting C++ to Javascript bungeeconnect

    Google V8 Shell Sample Code

    Google V8 Header File

    V8 Users Mailing List Can you populate a v8::Array from C++? Thread

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
i got an object with contents of html markup in it, for example: string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.