Created
March 26, 2012 20:53
-
-
Save eklitzke/2209598 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * A FunctionTemplate is used to create functions at runtime. There | |
| * can only be one function created from a FunctionTemplate in a | |
| * context. The lifetime of the created function is equal to the | |
| * lifetime of the context. So in case the embedder needs to create | |
| * temporary functions that can be collected using Scripts is | |
| * preferred. | |
| * | |
| * A FunctionTemplate can have properties, these properties are added to the | |
| * function object when it is created. | |
| * | |
| * A FunctionTemplate has a corresponding instance template which is | |
| * used to create object instances when the function is used as a | |
| * constructor. Properties added to the instance template are added to | |
| * each object instance. | |
| * | |
| * A FunctionTemplate can have a prototype template. The prototype template | |
| * is used to create the prototype object of the function. | |
| * | |
| * The following example shows how to use a FunctionTemplate: | |
| * | |
| * \code | |
| * v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); | |
| * t->Set("func_property", v8::Number::New(1)); | |
| * | |
| * v8::Local<v8::Template> proto_t = t->PrototypeTemplate(); | |
| * proto_t->Set("proto_method", v8::FunctionTemplate::New(InvokeCallback)); | |
| * proto_t->Set("proto_const", v8::Number::New(2)); | |
| * | |
| * v8::Local<v8::ObjectTemplate> instance_t = t->InstanceTemplate(); | |
| * instance_t->SetAccessor("instance_accessor", InstanceAccessorCallback); | |
| * instance_t->SetNamedPropertyHandler(PropertyHandlerCallback, ...); | |
| * instance_t->Set("instance_property", Number::New(3)); | |
| * | |
| * v8::Local<v8::Function> function = t->GetFunction(); | |
| * v8::Local<v8::Object> instance = function->NewInstance(); | |
| * \endcode | |
| * | |
| * Let's use "function" as the JS variable name of the function object | |
| * and "instance" for the instance object created above. The function | |
| * and the instance will have the following properties: | |
| * | |
| * \code | |
| * func_property in function == true; | |
| * function.func_property == 1; | |
| * | |
| * function.prototype.proto_method() invokes 'InvokeCallback' | |
| * function.prototype.proto_const == 2; | |
| * | |
| * instance instanceof function == true; | |
| * instance.instance_accessor calls 'InstanceAccessorCallback' | |
| * instance.instance_property == 3; | |
| * \endcode | |
| * | |
| * A FunctionTemplate can inherit from another one by calling the | |
| * FunctionTemplate::Inherit method. The following graph illustrates | |
| * the semantics of inheritance: | |
| * | |
| * \code | |
| * FunctionTemplate Parent -> Parent() . prototype -> { } | |
| * ^ ^ | |
| * | Inherit(Parent) | .__proto__ | |
| * | | | |
| * FunctionTemplate Child -> Child() . prototype -> { } | |
| * \endcode | |
| * | |
| * A FunctionTemplate 'Child' inherits from 'Parent', the prototype | |
| * object of the Child() function has __proto__ pointing to the | |
| * Parent() function's prototype object. An instance of the Child | |
| * function has all properties on Parent's instance templates. | |
| * | |
| * Let Parent be the FunctionTemplate initialized in the previous | |
| * section and create a Child FunctionTemplate by: | |
| * | |
| * \code | |
| * Local<FunctionTemplate> parent = t; | |
| * Local<FunctionTemplate> child = FunctionTemplate::New(); | |
| * child->Inherit(parent); | |
| * | |
| * Local<Function> child_function = child->GetFunction(); | |
| * Local<Object> child_instance = child_function->NewInstance(); | |
| * \endcode | |
| * | |
| * The Child function and Child instance will have the following | |
| * properties: | |
| * | |
| * \code | |
| * child_func.prototype.__proto__ == function.prototype; | |
| * child_instance.instance_accessor calls 'InstanceAccessorCallback' | |
| * child_instance.instance_property == 3; | |
| * \endcode | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment