Home > front end >  Js, how to find a new object
Js, how to find a new object

Time:03-03

.
The function Xtool (id, MSG) {
This. Id=id;
This. MSG=MSG;
}
.

New Xtool (" x1 ", "1234567")

Then the js, how to find a object?
Can you find like this? Eval (" x1 "), the document. The getElementById (" x1 ")
But can't. How do I find?

CodePudding user response:

A custom object is not attribute search by id, only new Xtool (" x1 ", "1234567"), the return value of the access object,

CodePudding user response:

Js object different from the dom tree, js object in memory don't you return address, how are you going to use

CodePudding user response:

A single object creation can use variables to store, var xtool=new xtool (' a ', '1111');
This xtool is on behalf of the newly created object

If you want to create multiple objects can use an array to store, can also use a dictionary to store, if id is not repeated dictionary is very convenient,

For example: dic [' a ']=new Xtool (' a ', '1111');
Dic (' a ') is a new object

CodePudding user response:

Understand, thank you.

Pressed by, how element to add methods, add event?
The function initMy () {
This style. Diasplay=""
.
}

Then can do this with
Adiv. InitMy ()

More events
Adiv. OnchangeStyle=function ()


CodePudding user response:

refer to 6th floor tom20803862 response:
understand, thank you.

Pressed by, how element to add methods, add event?
The function initMy () {
This style. Diasplay=""
.
}

Then can do this with
Adiv. InitMy ()

More events
Adiv. OnchangeStyle=function ()



With the apply or call to the caller, otherwise this point to incorrect
Or use initMy (Adiv) to invoke the function initMy (obj) {obj. Ooo. XXXX... }

CodePudding user response:

 
The function initMy (ele) {
This. Ele=ele;

this.init();
Enclosing regEvent ();
}
InitMy. Prototype. The init=function () {
This. Ele. Style. The display='
}
InitMy. Prototype. RegEvent=function () {
This. Ele. Onclick=function () {}
}

CodePudding user response:

Real element to add methods

 & lt; Div id="Adiv" & gt; 
ZZZZZZZZZ

HTMLElement. Prototype. InitMy=function () {
This. Style. BackgroundColor="red";
}

Document. The getElementById (" Adiv "). InitMy ();
</script>