简体   繁体   English

有关javascript原型的几个问题

[英]A few questions about the javascript prototype

1) Do html elements have prototypes or is it just javascript object? 1)html元素是否具有原型?还是只是javascript对象? In other words, would something like this be possible in any sort of way? 换句话说,这样的事情有可能以任何方式实现吗?

<div class="wrapper>
    <img src=""mypicture.png />

    <p>Click here</p>
</div>


.wrapper
{width:200px; height:50px;}

img
{width:100%;height:100%;}

p
{display:inline-block;height:100%;width:100%;}

And then in JS/jQuery something like this: 然后在JS / jQuery中,如下所示:

p.prototype = img;
$.on("click", "p", function(){
    $(this).prototype.trigger("click");
});

2) Say I'm using prototypes for a specific plugin I'm writing. 2)假设我正在为要编写的特定插件使用原型。 I'm not at the moment but just for informational purposes. 我目前不在,仅用于提供信息。 Say, in this plugin I'm using the JS integral Image() object and i decide, for whatever reason, to assign it a different prototype or add something to the prototype. 说,在这个插件中,我使用的是JS积分Image()对象,无论出于何种原因,我都决定为其分配一个不同的原型或向该原型添加一些东西。 Would I have to "undo" all the prototype work I did when the plugin gets exited so as to not interfere with anything else on the website that might be using the Image() object. 我必须“撤消”退出插件时所做的所有原型工作,以免干扰网站上可能使用Image()对象的任何其他事情。 Something like garbage collecting in other OOP languages. 类似于其他OOP语言中的垃圾收集。 Example: 例:

---Plugin start ---插件启动

Image.prototype = foobar;
 var image = new Image();
 ///
    do code here
 ///

on("plugin close ", function (){
     Image.prototype.release;
     OR
     Image.prototype = assign back to whatever it was originaly?
});

--- Plugin End -插件端

Here is what I think the answers might be: 1) NO!! 我认为答案可能是:1)不! But hopefully yes... 2) No prototype resetting isn't necesarry because if a jQuery plugin is wrapped inside of an anonymous function that that instance of code exists only as that one instance and it doesn't affec the global Image() object, just the local instance of the Image() object. 但是希望是... 2)不需要重置原型,因为如果将jQuery插件包装在一个匿名函数中,则该代码实例仅作为该实例存在,并且不会影响全局Image()对象。 ,仅是Image()对象的本地实例。

I'm just now trying to get into prototypes. 我现在正试图进入原型。 I've done a good amout of research and I def see the benefits of using them but before I jump into them like a fool, I would like to know exactly how to use the properly and what are the things I can and cannot do. 我已经做了大量的研究,并且我不知道使用它们的好处,但是在我像傻瓜一样跳进去之前,我想确切地知道如何正确地使用它们以及我可以做和不能做的事情。

Thx again guys! 再次谢谢你们!

About customizing HTML element behaviors , that wouldn't work as you showed in your code, but there's an incoming standard that can be partially used today to do so. 关于自定义HTML元素的行为 ,这不能像您在代码中显示的那样起作用,但是有一个传入的标准可以部分用于实现此目的。 Check this article to learn more about custom elements. 检查本文以了解有关自定义元素的更多信息。

In the other hand, about your sample of Image and adding members to its prototype, I really believe you need to understand scoping. 另一方面,关于您的Image样本以及将成员添加到其原型中,我真的相信您需要了解范围。 If you change a global object prototype, you're modifying its prototype either if you do that in the global or in some local scope. 如果更改全局对象原型,则在全局范围或局部范围内修改它的原型。

Thus, you need to inherit Image and add behavior in a new object: 因此,您需要继承 Image并在新对象中添加行为:

var CustomImage = function() {};
CustomImage.prototype = new Image();

CustomImage.doSomething = function() {

};

Or... 要么...

var customImageObject = Object.create(Image.prototype, {
     doSomething: {
         value: function() { }
     }
});

Anyway, that idea about "resetting prototypes" is a very very very bad idea! 无论如何, 关于“重置原型”的想法是一个非常非常非常糟糕的想法! . Play with prototype chain! 玩原型链!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM