A method and apparatus for implementing private properties of a JavaScript object using a template
By implementing private properties of JavaScript objects through native JavaScript templates, and utilizing initialization definition interfaces, virtual object modules, and property hijackers, the compatibility and support issues of TypeScript and the ES2020 standard draft are resolved. This achieves native support for private properties of JavaScript objects and a concise syntax, supporting class inheritance and object-oriented programming.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- CHENGDU SUOJI TECH CO LTD
- Filing Date
- 2022-10-13
- Publication Date
- 2026-08-04
AI Technical Summary
In existing technologies, the implementation of JavaScript object private properties using TypeScript has poor universality and compatibility. Most browsers do not support the implementation of the ES2020 standard draft, which makes it impossible to effectively apply JavaScript object private properties in browsers.
By using native JavaScript templates, including initialization definition interfaces, virtual object modules, and property hijackers, private properties of JavaScript objects are implemented. The virtual object module proxies the base class functionality, and the property hijacker completes property inheritance, ensuring native support in all browsers.
It implements native support for private properties of JavaScript objects, improving versatility and compatibility, simplifying syntax, supporting class inheritance and object-oriented programming, and does not depend on external libraries.
Smart Images

Figure CN115543272B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a method and apparatus for implementing private properties of JavaScript objects using templates, belonging to the field of computer technology. Background Technology
[0002] JavaScript is currently widely used not only in traditional web front-end and back-end applications, but in recent years its application has also expanded to desktop and mobile applications, becoming a comprehensive cross-platform language.
[0003] However, because JavaScript is a scripting language, it lacks some useful features compared to other high-level languages, such as a complete mechanism for protecting internal object data. Most high-level languages, such as Java and C++, provide the `private` access modifier to restrict access to internal data and method members of an object. Members declared as private can only be accessed by other members within the object (class); instantiated objects and inherited sub-objects cannot access these members. This mechanism allows the object implementation to hide underlying logic, facilitating data encapsulation and business logic isolation. The mechanism of private object properties is crucial for using OOP (Object-Oriented Programming).
[0004] To implement private properties of JavaScript objects, the current technical means mainly include using "TypeScript" and "ES2020 standard draft".
[0005] TypeScript syntax: As a superset of JavaScript, TypeScript uses the `private` keyword to implement object-private properties in the traditional sense of high-level languages. However, TypeScript was implemented by a commercial company and was not adopted by the ES standard. TypeScript code must also be compiled into native JavaScript code by a dedicated compiler before it can be used by browsers, so its versatility and compatibility are not very good.
[0006] The ES2020 standard draft proposes an implementation of private properties similar to TypeScript syntax, using the `#` keyword to achieve object-private properties in the traditional high-level language sense. However, ES2020 is still in the draft stage and has not yet been approved for implementation, meaning that most browsers do not yet support this feature.
[0007] The applicant has discovered that the prior art has at least the following technical problems:
[0008] 1. In existing technologies, the TypeScript approach has poor universality and compatibility. Current browsers do not natively support its syntax, and it needs to be compiled into native syntax by a special compiler before use.
[0009] 2. In the existing technology, the implementation of private object properties introduced by the ES2020 standard is not supported by most browsers because it has not yet become an official standard. Summary of the Invention
[0010] The purpose of this invention is to provide a method and apparatus for implementing private properties of JavaScript objects using native JavaScript language templates, in order to solve the technical problems of poor universality and compatibility of the existing TypeScript approach and the fact that most browsers do not support the implementation of private properties of objects introduced by the ES2020 standard.
[0011] To achieve the above objectives, the present invention provides the following technical solution:
[0012] This invention provides a device for implementing private properties of JavaScript objects using native JavaScript templates. The device includes an initialization definition interface, a virtual object module, and a property hijacker.
[0013] The initialization definition interface is used to construct subclasses, base classes, and the inheritance relationship between the subclasses and base classes;
[0014] The virtual object module is used to proxy the functionality of the base class constructed by the initialization definition interface;
[0015] The attribute hijacker is used to implement the attribute inheritance of the base class by the subclass constructed by the initialization definition interface.
[0016] Furthermore, the initialization definition interface constructs the subclass, base class, and inheritance relationship between the subclass and base class by receiving the subclass name, base class name, subclass implementation function, and base class implementation function input by the user.
[0017] Furthermore, the way in which the virtual object module proxy implements the functionality of the base class constructed by the initialization definition interface includes: implementing a virtual object in the runtime space of the device, and implementing the functionality of all attributes of the base class constructed by the initialization definition interface through the virtual object proxy.
[0018] Furthermore, the method by which the attribute hijacker implements the subclass of the initialization definition interface to inherit attributes from the base class includes:
[0019] Instantiate the base class constructed by the initialization definition interface and enable the base class constructed by the initialization definition interface to obtain runtime space;
[0020] The virtual object proxy implemented by the virtual object module enables the implementation of the functionality of all attributes of the base class constructed by the initialization definition interface;
[0021] The virtual object implemented by the virtual object module is registered in the browser's runtime space with the subclass name entered by the user and then run, so that the subclass is instantiated and inherits the attributes of the base class through the virtual object.
[0022] Furthermore, the virtual object implements the functionality of all attributes of the base class constructed by the initialization definition interface in the following ways:
[0023] The attribute hijacker links the base class constructed by the initialization definition interface with the virtual object implemented by the virtual object module, so that the behavior of the virtual object implemented by the virtual object module is completely consistent with that of the base class constructed by the initialization definition interface.
[0024] The attribute hijacker hijacks the attributes of the base class constructed by the initialization definition interface to the virtual object, thereby completing the attribute inheritance of the base class constructed by the initialization definition interface.
[0025] Furthermore, the attribute hijacker links the base class constructed by the initialization definition interface with the virtual object implemented by the virtual object module through prototype chain hijacking. The attribute hijacker hijacks the attributes of the base class constructed by the initialization definition interface to the prototype chain of the virtual object, thus completing the attribute inheritance of the base class constructed by the initialization definition interface.
[0026] Furthermore, the device is implemented based on JavaScript class templates.
[0027] This invention also provides a method for implementing private properties of JavaScript objects using templates, based on the aforementioned apparatus for implementing private properties of JavaScript objects using templates. The method includes the following steps:
[0028] S1, construct the subclass, base class, and inheritance relationship between the subclass and base class through the initialization definition interface. The construction method includes the user passing the subclass name, base class name, subclass implementation function, and base class implementation function to the initialization definition interface.
[0029] S2, virtual objects are implemented in the operating space of the device through the virtual object module;
[0030] S3, The attribute hijacker instantiates the base class constructed by the initialization definition interface;
[0031] S4, enabling the virtual object proxy to implement the functionality of the base class constructed by the initialization definition interface;
[0032] S5, the attribute hijacker enables the subclass constructed by the initialization definition interface to inherit the attributes of the base class.
[0033] Furthermore, the sub-steps of step S4 include:
[0034] S41, the base class constructed by the initialization definition interface is linked to the virtual object through the prototype chain, so that the behavior of the virtual object is completely consistent with that of the base class constructed by the initialization definition interface;
[0035] S42, through the attribute hijacker, the attributes of the base class constructed by the initialization definition interface are hijacked to the prototype chain of the virtual object, thereby completing the attribute inheritance of the base class constructed by the initialization definition interface.
[0036] Furthermore, step S5, which uses an attribute hijacker to implement the inheritance of attributes of the base class by the subclass constructed by the initialization definition interface, includes the following methods:
[0037] The virtual object is registered in the browser's runtime space with the subclass name entered by the user and run by the attribute hijacker, so that the subclass is instantiated and obtains the attribute inheritance of the base class through the virtual object.
[0038] Furthermore, in step S1, a base class is first defined, and then subclasses that inherit from the base class are defined.
[0039] Based on the above technical solution, the embodiments of the present invention can produce at least the following technical effects:
[0040] (1) The present invention provides a method and apparatus for implementing private properties of JavaScript objects using templates, which are created using native JavaScript syntax and can be used directly by all browsers natively.
[0041] (2) The present invention provides a method and apparatus for implementing private properties of JavaScript objects using templates. It has good portability, does not depend on any external library, and is easy to introduce and use.
[0042] (3) The present invention provides a method and apparatus for implementing private properties of JavaScript objects using templates, which supports defining all public and private properties in the template, making it convenient to use and with a concise and elegant syntax.
[0043] (4) The present invention provides a method and apparatus for implementing private properties of JavaScript objects using templates, which supports class inheritance and facilitates the use of object-oriented programming methods in program design. Attached Figure Description
[0044] Figure 1 This is a schematic diagram of the structure of an embodiment of the present invention;
[0045] Figure 2 This is a schematic diagram of the method flow according to an embodiment of the present invention. Detailed Implementation
[0046] This invention provides a method and apparatus for implementing private properties of JavaScript objects using templates. To further clarify the purpose, technical features, and advantages of this invention, preferred embodiments are now clearly and completely described in conjunction with the accompanying drawings. Obviously, the embodiments described in this section are only some, not all, of the embodiments of this invention.
[0047] This invention provides an apparatus for implementing private properties of JavaScript objects using templates. The apparatus includes an initialization definition interface, a virtual object module, and a property hijacker.
[0048] The initialization definition interface is used to construct subclasses, base classes, and the inheritance relationship between the subclasses and base classes;
[0049] The virtual object module is used to proxy the functionality of the base class constructed by the initialization definition interface;
[0050] The attribute hijacker is used to implement the attribute inheritance of the base class by the subclass constructed by the initialization definition interface.
[0051] In a preferred embodiment of the present invention, the initialization definition interface constructs the subclass, base class, and inheritance relationship between the subclass and base class by receiving the subclass name, base class name, subclass implementation function, and base class implementation function input by the user.
[0052] In step S1, first define the base class, then define the subclasses that inherit from the base class. The "Subclass Name" is the name of the subclass to be defined. If the subclass is an independent class without a base class (parent class), the "Base Class Name" parameter after the subclass should be left blank to indicate that there is no base class; the "Base Class Name" is the name of the "Base Class (Parent Class)" of the subclass to be defined. Before using this parameter, you must ensure that the "Base Class (Parent Class)" has already been defined using this template, and that the defined names are consistent.
[0053] In a preferred embodiment of the present invention, the method by which the virtual object module proxy implements the functionality of the base class constructed by the initialization definition interface includes: implementing a virtual object in the runtime space of the device, and implementing the functionality of the base class constructed by the initialization definition interface through the virtual object proxy.
[0054] In a preferred embodiment of the present invention, the attribute hijacker implements the subclass inheritance of the base class's attributes by the initialization definition interface construction in the following ways:
[0055] Instantiate the base class constructed by the initialization definition interface and enable the base class constructed by the initialization definition interface to obtain runtime space;
[0056] The virtual object proxy implemented by the virtual object module enables the functionality of the base class constructed by the initialization definition interface;
[0057] The virtual object implemented by the virtual object module is registered in the browser's runtime space with the subclass name entered by the user and then run, so that the subclass is instantiated and inherits the attributes of the base class through the virtual object.
[0058] In a preferred embodiment of the present invention, the virtual object implements the functionality of the base class constructed by the initialization definition interface in the following ways:
[0059] The attribute hijacker links the base class constructed by the initialization definition interface with the virtual object implemented by the virtual object module, so that the behavior of the virtual object implemented by the virtual object module is completely consistent with that of the base class constructed by the initialization definition interface.
[0060] The attribute hijacker hijacks the attributes of the base class constructed by the initialization definition interface to the virtual object, thereby completing the attribute inheritance of the base class constructed by the initialization definition interface.
[0061] In a preferred embodiment of the present invention, the attribute hijacker links the base class constructed by the initialization definition interface with the virtual object implemented by the virtual object module through prototype chain hijacking. The attribute hijacker hijacks the attributes of the base class constructed by the initialization definition interface to the prototype chain of the virtual object, thereby completing the attribute inheritance of the base class constructed by the initialization definition interface.
[0062] In a preferred embodiment of the present invention, the device is implemented based on JavaScript class templates.
[0063] This invention also provides a method for implementing private properties of JavaScript objects using templates, based on the aforementioned apparatus for implementing private properties of JavaScript objects using templates. The method includes the following steps:
[0064] S1, construct the subclass, base class, and inheritance relationship between the subclass and base class through the initialization definition interface. The construction method includes the user passing the subclass name, base class name, subclass implementation function, and base class implementation function to the initialization definition interface.
[0065] S2, virtual objects are implemented in the operating space of the device through the virtual object module;
[0066] S3, The attribute hijacker instantiates the base class constructed by the initialization definition interface;
[0067] S4, enabling the virtual object proxy to implement the functionality of the base class constructed by the initialization definition interface;
[0068] S5, the attribute hijacker enables the subclass constructed by the initialization definition interface to inherit the attributes of the base class.
[0069] In a preferred embodiment of the present invention, step S4 includes the following sub-steps:
[0070] S41, the base class constructed by the initialization definition interface is linked to the virtual object through the prototype chain, so that the behavior of the virtual object is completely consistent with that of the base class constructed by the initialization definition interface;
[0071] S42, through the attribute hijacker, the attributes of the base class constructed by the initialization definition interface are hijacked to the prototype chain of the virtual object, thereby completing the attribute inheritance of the base class constructed by the initialization definition interface.
[0072] In a preferred embodiment of the present invention, step S5, which uses an attribute hijacker to implement the inheritance of attributes of the base class by the subclass constructed by the initialization definition interface, includes:
[0073] The virtual object is registered in the browser's runtime space with the subclass name entered by the user and run by the attribute hijacker, so that the subclass is instantiated and obtains the attribute inheritance of the base class through the virtual object.
[0074] In a preferred embodiment of the present invention, the above-mentioned functions are implemented using a JavaScript class template named $class.
[0075] The following is a sample code example of implementing a class template:
[0076]
[0077]
[0078] In practical use, follow these steps:
[0079] Prerequisites:
[0080] 1. Base class name: BaseCls
[0081] 2. Subclass name: ChildCls
[0082] 3. Class template name: $class is provided to users as a library. The user implementation steps are as follows.
[0083] Step 1: Define the "base class name" and pass the base class implementation function to the "initialization definition interface". For simplicity in this example, the base class implementation function is implemented directly in the `$class` parameter as a closure. Design a private data variable: `privateData`;
[0084] Design a function to read a private variable: getPrivaetData();
[0085] Sample code is as follows:
[0086]
[0087] Step 2: Define the "subclass name" and pass the subclass implementation function to the "initialization definition interface". For simplicity in this example, the subclass implementation function is implemented directly in the `$class` parameter as a closure. Design a data variable: `chdPrivateData`;
[0088] Design a function to read private variables: getChdPrivate();
[0089] Sample code is as follows:
[0090]
[0091]
[0092] Step 3: Instantiate the base class and subclasses to start running and using them.
[0093] Sample code is as follows:
[0094] varp=newBaseCls('Jack',23);
[0095] varm=newChildCls('Tom',25);
[0096] Verification results:
[0097] Public properties can be accessed directly using the object extraction operator ".";
[0098] Private properties can only be accessed through interface functions provided by the object, and cannot be accessed directly.
[0099] Subclasses inherit the public and private attributes of the base class, and their access characteristics are the same as described above;
[0100] Private properties of a subclass can only be accessed through interface functions provided by the object, and cannot be accessed directly.
[0101] The result fully achieved the design objectives.
[0102] The following is a sample code for the verification result:
[0103] varp=newBaseCls('Jack',23);
[0104] console.log("Public properties can be accessed directly this.publicData="+ppublicData);
[0105] console.log("Private data cannot be accessed using the dot operator"+p.privateData);
[0106] console.log("Private properties can only be accessed through interface functions getPrivateData="+p.getPrivateData());
[0107] varm=newChildCls('Tom',25);
[0108] console.log("Public properties inherited from the base class can be accessed directly using the dot operator >>" + m.publicData);
[0109] console.log("Private properties of the base class cannot be directly accessed using the dot operator >>" + m.privateData);
[0110] console.log("The private property reading function inherited from the base class, getPrivateData>>"+m.getPrivateData());
[0111] console.log("The function to read the private property of the subclass is chd_private>>"+m.getChdAge());
[0112] The above steps complete all the operations of this invention.
[0113] In particular, if there is no requirement for subclass inheritance, all the definitions can be completed in just the first step before using the actual object.
[0114] It is understood that this invention has been described through this preferred embodiment, and those skilled in the art will recognize that various changes or equivalent substitutions can be made to these features and this embodiment without departing from the spirit and scope of this invention. Furthermore, under the teachings of this invention, these features and this embodiment can be modified to adapt to specific circumstances without departing from the spirit and scope of this invention. Therefore, this invention is not limited to the specific embodiments disclosed herein, and all embodiments falling within the scope of the claims of this application are within the protection scope of this invention.
Claims
1. An apparatus for implementing private properties of JavaScript objects using templates, characterized in that, The device includes an initialization definition interface, a virtual object module, and an attribute hijacker. The initialization definition interface is used to construct subclasses, base classes, and the inheritance relationship between the subclasses and base classes. The virtual object module is used to proxy the functionality of the base class constructed by the initialization definition interface; The attribute hijacker is used to implement attribute inheritance of the subclasses constructed by the initialization definition interface; The attribute hijacker implements attribute inheritance of the subclass constructed by the initialization definition interface in the following ways: instantiating the base class constructed by the initialization definition interface and enabling the base class constructed by the initialization definition interface to obtain runtime space; The virtual object proxy implemented by the virtual object module enables the implementation of the functionality of all attributes of the base class constructed by the initialization definition interface; The virtual object implemented by the virtual object module is registered in the browser's runtime space with the subclass name entered by the user and then run, so that the subclass is instantiated and inherits the attributes of the base class through the virtual object. The way in which the virtual object proxy implements the functionality of all attributes of the base class constructed by the initialization definition interface includes: the attribute hijacker links the base class constructed by the initialization definition interface with the virtual object implemented by the virtual object module, so that the behavior of the virtual object implemented by the virtual object module is completely consistent with that of the base class constructed by the initialization definition interface; The attribute hijacker hijacks the attributes of the base class constructed by the initialization definition interface to the virtual object, thereby completing the attribute inheritance of the base class constructed by the initialization definition interface. The attribute hijacker links the base class constructed by the initialization definition interface with the virtual object implemented by the virtual object module through prototype chain hijacking. The attribute hijacker hijacks the attributes of the base class constructed by the initialization definition interface to the prototype chain of the virtual object, thus completing the attribute inheritance of the base class constructed by the initialization definition interface.
2. The apparatus for implementing private properties of JavaScript objects using templates according to claim 1, wherein, The initialization definition interface constructs the subclass, base class, and inheritance relationship between the subclass and base class by receiving the user's input of the subclass name, base class name, subclass implementation function, and base class implementation function.
3. The apparatus for implementing private properties of JavaScript objects using templates according to claim 2, wherein, The method by which the virtual object module proxy implements the functionality of the base class constructed by the initialization definition interface includes: implementing a virtual object in the runtime space of the device, and implementing the functionality of the base class constructed by the initialization definition interface through the virtual object proxy.
4. The apparatus for implementing private properties of JavaScript objects using templates according to claim 1, wherein, The device is implemented based on JavaScript class templates.
5. A method for implementing private properties of JavaScript objects using templates, implemented based on the apparatus for implementing private properties of JavaScript objects using templates according to any one of claims 1 to 4, characterized in that, The steps of the method include: S1, construct the subclass, base class, and inheritance relationship between the subclass and base class through the initialization definition interface. The construction method includes the user passing the subclass name, base class name, subclass implementation function, and base class implementation function to the initialization definition interface. S2, virtual objects are implemented in the operating space of the device through the virtual object module; S3, The attribute hijacker instantiates the base class constructed by the initialization definition interface; S4, enabling the virtual object proxy to implement the functionality of the base class constructed by the initialization definition interface; S5, the attribute hijacker enables the subclass constructed by the initialization definition interface to inherit the attributes of the base class.
6. The method for implementing private properties of JavaScript objects using templates according to claim 5, wherein, The sub-steps of step S4 include: S41, the base class constructed by the initialization definition interface is linked to the virtual object through the prototype chain, so that the behavior of the virtual object is completely consistent with that of the base class constructed by the initialization definition interface; S42, through the attribute hijacker, the attributes of the base class constructed by the initialization definition interface are hijacked to the prototype chain of the virtual object, thereby completing the attribute inheritance of the base class constructed by the initialization definition interface.
7. The method for implementing private properties of JavaScript objects using templates according to claim 5, wherein, Step S5, which implements the inheritance of base class attributes by the subclass constructed by the initialization definition interface through the attribute hijacker, includes: registering the virtual object with the user-input subclass name in the browser's runtime space and running it through the attribute hijacker, so that the subclass is instantiated and inherits the base class attributes through the virtual object.