JavaScript基础对象参数继承机制及Wikitude中onTargetsLoaded来源问询
onTargetsLoaded 的疑问解答 一、JavaScript 基础 Object 对象的继承逻辑
Great questions! Let's start with the Object inheritance part.
In JavaScript, every plain object you create (like {} or new Object()) inherits built-in properties and methods from Object.prototype via the prototype chain. That's why all objects have access to things like toString(), hasOwnProperty(), or valueOf()—those are baked into the base Object prototype.
When you mention "inheriting parameters," I think you might be mixing up two scenarios:
- If you're talking about initializing an Object with a value (like
new Object("test")), this just creates a wrapper object for the primitive value, and it still inherits fromObject.prototypelike any other object. - If you mean how one object inherits custom properties from another, that's prototype inheritance (not directly tied to the base Object itself). For example,
const childObj = Object.create(parentObj)makeschildObj's prototype point toparentObj, letting it accessparentObj's properties—whileparentObjstill inherits fromObject.prototypefurther up the chain.
二、Wikitude 中 onTargetsLoaded 的来源解析
Now onto your Wikitude code confusion—this is a super common pattern in JS libraries, so let's break it down.
The onTargetsLoaded property has nothing to do with inheriting from Object. Instead, it's a custom property you (or the code you're looking at) define in a plain object that gets passed to a Wikitude API method. Here's the breakdown:
Wikitude's AR SDK uses an "options object" pattern. When you call certain SDK methods (like one that loads image targets), you pass a plain object with pre-agreed properties—onTargetsLoaded is one of these agreed-upon keys. The SDK's internal code checks for this property, and if it's a function, it calls it once the targets finish loading.
Think of this simplified example to get the idea:
// Hypothetical Wikitude SDK method function loadARTargets(configOptions) { // Internal code to load targets from your source... // Once loading completes: if (typeof configOptions.onTargetsLoaded === 'function') { configOptions.onTargetsLoaded(); // Run your custom callback } } // How you'd call it in your code loadARTargets({ onTargetsLoaded: function() { console.log('All AR targets loaded successfully!'); }, // Other optional config properties the SDK expects... });
Key clarifications here:
- That plain object doesn't "inherit"
onTargetsLoaded—you're explicitly adding it as a custom property. JavaScript lets you attach any property to plain objects, no inheritance required. - You mentioned thinking this parameter "only exists in AR plugin classes"—that's a misunderstanding. Wikitude doesn't force you to inherit from a class with this property; instead, it just expects you to pass an object that includes this callback (if you want to handle the loaded event). It's an API convention, not an inherited class property.
内容的提问来源于stack exchange,提问作者user3355139

