实例化扩展的浏览器对象 [英] Instantiate extended browser objects

查看:155
本文介绍了实例化扩展的浏览器对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于一个大项目,我想创建不同的javascript类

并将它们分配给div。但是怎么样? :)

我知道原型的用法,但考虑到这是可能的:


功能newDiv(){...}

newDiv.prototype = new div();


当然不是。如何实例化该类?


var newDiv_instance = document.createElement(" div");

newDiv_instance.prototype = new newDiv();

关于Div'扩展的任何建议/资源?

谢谢,chr

Due a big project I would like to create different javascript classes
and assign them to divs. But how? :)
I know the usage of prototype but given that this could be possible:

function newDiv(){...}
newDiv.prototype=new div();

and of course it isn''t. How to instantiate that class?

var newDiv_instance=document.createElement("div");
newDiv_instance.prototype=new newDiv();
Any suggestions/resource about the extension of Div''s?
Thanks, chr

推荐答案




gabon写道:


gabon wrote:
由于一个大项目我想创建不同的javascript类
并分配他们去div。


JavaScript课程?你最好先解释一下你理解的内容

" JavaScript class"是。

并为div分配课程?

我知道原型的使用但是考虑到这可能是:

功能newDiv( ){...}
newDiv.prototype = new div();

当然不是。如何实例化该类?

var newDiv_instance = document.createElement(" div");
newDiv_instance.prototype = new newDiv();
Due a big project I would like to create different javascript classes
and assign them to divs.
JavaScript classes? You would better explain then what you understand a
"JavaScript class" to be.
And assigning classes to divs?
I know the usage of prototype but given that this could be possible:

function newDiv(){...}
newDiv.prototype=new div();

and of course it isn''t. How to instantiate that class?

var newDiv_instance=document.createElement("div");
newDiv_instance.prototype=new newDiv();




也许你想要

函数newDiv(){...}

newDiv.prototype = document.createElement(''div'');

那会(用HTML文档中的脚本和W3C DOM支持)

使构造函数newDiv的原型对象成为新的

创建的HTMLDivElement主客体。因此,使用

var customDiv = new newDiv()

创建的任何对象在其原型链中都具有该原型对象。

但是不会以任何方式将自定义div对象插入到

a HTML文档中,你需要一个宿主对象。


也许你想要它的另一种方式圆形,例如

if(typeof HTMLDivElement!=''undefined''&& typeof

HTMLDivElement.prototype!=''undefined''){

HTMLDivElement.prototype = new newDiv();

}

但只有Mozilla和最近的Opera(8)公开HTMLDivElement而且只有

Mozilla它的原型,我不认为更换这种

a主机对象的原型是一个好主意。


XBL可能是你可以做的事情查看Mozilla唯一的解决方案

将行为绑定到元素或构建自定义小部件:

< http://developer.mozilla.org/en/docs/XBL>


- -


Martin Honnen
http:// JavaScript.FAQTs.com/




gabon写道:

gabon wrote:
由于大项目我想创建不同的javascript类
并将它们分配给div。但是怎么样? :)
我知道原型的用法,但考虑到这是可能的:

函数newDiv(){...}
newDiv.prototype = new div();

当然不是。如何实例化该类?

var newDiv_instance = document.createElement(" div");
newDiv_instance.prototype = new newDiv();

任何建议/关于Div'扩展的资源?

谢谢,chr


嗨。


我想你需要更清楚地知道为什么你需要这样做。你是否想要为DIVS创建方法?


我对此的了解有限,但我知道以下内容: -

newDiv_instance.prototype = new newDiv();
Due a big project I would like to create different javascript classes
and assign them to divs. But how? :)
I know the usage of prototype but given that this could be possible:

function newDiv(){...}
newDiv.prototype=new div();

and of course it isn''t. How to instantiate that class?

var newDiv_instance=document.createElement("div");
newDiv_instance.prototype=new newDiv();
Any suggestions/resource about the extension of Div''s?
Thanks, chr
Hi.

I think you need to be clearer as to why you need to do this. Do you
want to create methods for the DIVS?

My knowledge on this is limited, but I am aware of the following:-
newDiv_instance.prototype=new newDiv();




1.作为一般起点,在Javascript中你无法访问

实例上的原型。原型与构造函数一起使用

来创建实例。


因此: -


函数C() {}

C.prototype = {};

var I = new C();

alert(I.prototype); // undefined


2.在Internet Explorer中,您无法访问HTML

元素的构造函数,因此无法扩展它们。

但是你可以为div实例添加属性,例如


newDiv_instance.myProperty = new Object();

newDiv_instance.myProperty2 = function (){alert(this。document;)};


但是这些仅针对该实例,而this

关键字将指向窗口对象。


3.在Mozilla(或许还有其他人)中,您可以访问构造函数

的HTML对象。尝试一些研究。


例如

HTMLElement.prototype.myCommonMethod = function(){al ert(this.nodeType);};


但请注意,HTMLElement的所有实例都可以访问它,

,当然不是跨浏览器。


Julian



1. As a general starting point, in Javascript you cannot access the
prototype on instances. The prototype is used with constructors used
to create instances.

Thus:-

function C(){}
C.prototype={};
var I=new C();
alert(I.prototype); // undefined

2. In Internet Explorer you cannot access the constructors of HTML
elements, so you cannot extend them.

However you can add properties to the div instances, e.g.

newDiv_instance.myProperty=new Object();
newDiv_instance.myProperty2=function(){alert(this. document;)};

However these will be specific to that instance only, and the "this"
keyword will point to the "window" object.

3. In Mozilla (and perhaps others) you can access the constructors
of HTML objects. Try some research on this.

E.g.
HTMLElement.prototype.myCommonMethod=function(){al ert(this.nodeType);};

But note that this will be accessible to all instances of HTMLElement,
and is of course not cross browser.

Julian


作为JavaScript类,我打算用javascript 创建一个类:


function newDiv(){

...

}

newDiv.prototype.myMethod = function(){

...

}

我想创建不同的类来分配给不同的div

,具体取决于他们应该做什么。 />
在OOP方法中,我认为这应该是最好的方法,而不是将所有方法分配给所有div或者实例化处理

的类。外部的div


函数newDiv(myDiv){

}

newDiv.prototype.moveDiv = function(){

this.myDiv.style.lef t =100px;

}


当然解决方案应该是非常跨平台的:(


你对什么进行了调查?

非常感谢任何建议,chr

As JavaScript class I intend a class created in javascript:

function newDiv(){
...
}
newDiv.prototype.myMethod=function(){
...
}
I would like to create different classes to assign to different divs
depending on what they should do.
In a OOP approach I think that should be the best way instead of assign
to all the divs all the methods or to instantiate classes that handle
externally the div

function newDiv(myDiv){
}
newDiv.prototype.moveDiv=function(){
this.myDiv.style.left="100px";
}

And of course the solution should be VERY cross-platform :(

What do you recon?
Thanks a lot for any suggestion, chr


这篇关于实例化扩展的浏览器对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆