简体   繁体   English

使用javascript时如何调用.toggle() function?

[英]How is .toggle() function being called when using javascript?

I am learning javascript and I have a box and i want to change its class name.我正在学习 javascript 并且我有一个盒子,我想更改它的 class 名称。 So,所以,

 var el=document.querySelectorAll('.myClass'); for(var x=0;x<el.length;x++){ el[x].onclick=function(){ // this.classList.toggle(box1); how is this.classList.toggle possible here? I am not seeing it when doing console.dir(this.classList) console.dir(this.classList); } }
 <style>.box1{ background-color: aqua; font-size: 1.5em; color: #fff; }.box2{ background-color: red; font-size: 0.5em; color: #ddd; } #one,#two,#three{ width: 100px; height: 100px; display: inline-block; border: 1px solid black; } </style>
 <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div id="one" class="myClass">Box 1</div> <div id="two" class="myClass">Box 2</div> <div id="three" class="myClass">Box 3</div> </body> </html>

When I click the box I am getting the current object in console.log(this);当我单击该框时,我在console.log(this); . . I saw the classList property.我看到了 classList 属性。 So I am fine upto console.dir(this.classList);所以我很满意console.dir(this.classList); .But,my tutor is using this.classList.toggle(box1); .但是,我的导师正在使用this.classList.toggle(box1); .I didnt understand how he is getting option to call .toggle() function there? .我不明白他如何选择调用.toggle() function 那里? When i looked at当我看着

 `console.log(this.classList);` 

I saw only我只看到

 DOMTokenList ["myClass", value: "myClass"]
    0: "myClass"
    length: 1
    value: "myClass"
    __proto__: DOMTokenList

But i didnt see any option of.toggle() there?但我没有看到任何选项 of.toggle() 那里? But how can we know that toggle can be used after this.classList since I am not seeing any property of toggle() function after this.classList ?但是我们怎么知道可以在this.classList之后使用切换,因为在this.classList之后我没有看到 toggle() function 的任何属性? Please clarify me.请澄清一下。

From MDN website :来自MDN 网站

When it comes to inheritance, JavaScript only has one construct: objects.说到 inheritance,JavaScript 只有一个结构:对象。 Each object has a private property which holds a link to another object called its prototype.每个 object 都有一个私有属性,该属性保存到另一个 object 的链接,称为其原型。 That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype.该原型 object 有自己的原型,依此类推,直到以 null 为原型达到 object。 By definition, null has no prototype, and acts as the final link in this prototype chain.根据定义,null 没有原型,是这个原型链中的最后一环。

Therefore to find what properties (functions, setters, getters.. etc) exist you can view them under __proto__ .因此,要查找存在哪些属性(函数、setter、getter 等),您可以在__proto__下查看它们。

console.log only logs enumerable properties . console.log只记录可枚举的属性 toggle isn't enumerable. toggle是不可枚举的。

 console.log("toggle" in document.body.classList); console.log(document.body.classList.propertyIsEnumerable("toggle"));

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

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