Home  >  Article  >  Web Front-end  >  Basic analysis of js writing method_javascript skills

Basic analysis of js writing method_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:11:43849browse

var a=false;
!a&&alert("hi");
Explanation: a is true before execution continues, so hi will pop up
var a=a||"hi";
Explanation When a is false, execution will continue. When a is true, this statement will be jumped out, so you can copy the default value through this method
Not all languages ​​​​are handled this way, such as PHP
$a= @$a||"hi";
echo $a;//1
php assigns the value after (@$a||"hi")...
$a=true ;
!$a&&echo "hi";
Report error directly: syntax error, unexpected T_ECHO
About prototype
In many cases, when I see prototype, I think of only objects.
var add= function(){alert("b");}
add.prototype.a = function () {alert("hi");}
new add.prototype.a;
First reaction, An error may be reported, but in fact, when there is no production object, you can directly call the static method through add.prototype.a
JS is quite misleading, cough
Selector problem:
Sometimes you need to select the child nodes under a node and use childNodes to get the value FIREFOX. The difference in IE is very frustrating
document.getElementsByTagName("head")[0].getElementsByTagName("script")[0];
The above method works well when used in the head tag stage, but not so useful when used in other areas
I think it’s better to use a class name

Copy code The code is as follows:

function getclassnode(classname,doc){
doc=doc||document;
var node=[] ,i=0,j=0,t;
var allnode=doc.getElementsByTagName("*");
while(t=allnode[i]){
if(RegExp(classname).test (t.className)){
node[j]=t;
j ;
}
i ;
}
return node;
}

Since using JQ, I have almost forgotten all the native JS operations. I review it occasionally, and it feels very troublesome and depressing.
I would like to add some things that I need to pay attention to in JS that I discovered today. If a JS has been After being imported into the current document, even if the imported node is removed, the variables, functions, etc. defined through the imported JS file are still valid because they have been loaded into the current document environment, as shown in the following code:
Copy code The code is as follows:

document.getElementsByTagName("head")[0].removeChild(document.getElementsByTagName("head")[0] .getElementsByTagName("script")[0]);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn