使用原型并处理执行顺序

编程入门 行业动态 更新时间:2024-10-24 04:48:05
本文介绍了使用原型并处理执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在努力掌握JavaScript,而且我遇到了一个大问题。我习惯使用C语言,我发现的一个障碍是处理JavaScript的原型功能以及声明函数时,因为它涉及执行的顺序。

I'm trying to get a really solid grasp of JavaScript and I'm stumbling across a big issue for me. I'm used to working in C languages and one of the barriers I'm finding is dealing with the prototype functionality of JavaScript and when functions are declared, as it is concerned with the order of execution.

例如,请使用以下代码:

For instance, take the following code:

var construct = new Constructor(); //I can do this even if its above the declaration of the object. construct.MyPrivilagedFunction(); //Can do this here too, even though it's above the function declaration. construct.MyPublicFunction(); //Can't do this because it is above the function declaration. function Constructor() { //Private member var m_Ding = "Ding!"; //Accessible publicly and has access to private member. this.MyPrivilagedFunction = function() { console.log(m_Ding); } } Constuctor.prototype.MyPublicFunction = function() { //Doesn't have access to private members. This doesn't work. console.log(m_Ding); }

我知道原型设计提供了更好的性能,因为这个功能的副本不是存储在对象的每个实例上,而不是每个实例都引用相同的函数(我猜每个新实例都可以被认为是一个全新的对象类型?)。但是,原型设计不允许我在定义之前使用函数。此外,原型函数无法访问对象的私有成员。

I understand that prototyping provides better performance because then a copy of the function is not stored on every instance of your object and instead every instance is referring to the same function (and I guess each new instance could be considered a whole new object type?). However, prototyping does not allow me to use a function before it is defined. Furthermore, a prototyped function doesn't have access to a private member of the object.

这只是一个问题,因为我正在处理一个项目,其中两个对象将需要使用彼此的功能。如果我在代码中先放置一个对象,它将无法访问第二个对象,因为原型函数遵循执行顺序(从上到下)。

This is only really a problem because I am working on a project where two objects will need to use each other's functions. If I place one object earlier in the code, it won't have access to the second object because prototyped functions adhere to the order of execution (top to bottom).

旁注:我也知道我的对象可能应该是一个对象文字(比如object = {property:value}),但是我仍然试图抓住范围和原型来试图解决这个问题。现在。

Side Note: I'm also aware that my object should probably be an object literal ( like object={property:value}), but I'm still trying to get a solid grasp on scope and prototyping to attempt to deal with that for now.

推荐答案

执行顺序是你写的。规则中只有两个例外,以下所有内容都是无关紧要的:变量和函数声明被提升,即您可以在上面使用(分配,调用)它们。并注意函数声明和函数表达式之间的区别。

The order of execution is the one you write. There are only two exceptions from the rule "everything below is irrelevant": variable and function declarations are hoisted, i.e. you can use (assign to, call) them above. And beware of the difference between function declarations and function expressions.

var construct = new Constructor()//即使它高于对象的声明,我也可以这样做。

你想说的构造函数的声明。

Declaration of the constructor function you want to say.

construct.MyPrivilagedFunction(); //这里也可以这样做,即使它在函数声明之上。

没有函数声明在这里。在执行构造函数期间创建了特权方法(通过向属性赋值函数)(参见上文)。

There is no function declaration in here. The privileged method was created (with an assignment of a function expression to a property) during the execution of the constructor function (see above).

construct.MyPublicFunction(); //不能这样做,因为它在函数声明之上。

同样,它不是函数声明但是函数表达式的赋值。并且它还没有发生,因为它在下面。

Again, it is no function declaration but an assignment of a function expression. And it didn't happen yet, because it is below.

这只是一个问题,因为我正在开展一个项目,其中两个对象需要使用彼此的功能。如果我在代码中先放置一个对象,它将无法访问第二个对象,因为原型函数遵循执行顺序(从上到下)。

This is only really a problem because I am working on a project where two objects will need to use each other's functions. If I place one object earlier in the code, it won't have access to the second object because prototyped functions adhere to the order of execution (top to bottom).

在打电话之前,通常不需要访问任何东西。首先声明所有内容(构造函数,原型属性),然后实例化。

You usually don't need access to anything before you call anything. "Declare" everything at first (constructor, prototype properties), then instantiate.

更多推荐

使用原型并处理执行顺序

本文发布于:2023-11-30 05:29:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648881.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:原型   顺序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!