Home >Web Front-end >JS Tutorial >How to write php code directly in javascript_javascript skills

How to write php code directly in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:27:391401browse

1. Embed php code in javascript
If javascript is included through a js file, then php code can also be written directly in the js file. However, if the js file is included, the extension must be changed to php, such as:

Copy code The code is as follows:



2. Default values ​​of javascript function parameters
In C language, you can set default parameters like this:
Copy code The code is as follows:

void foo(int a, int b = 1, bool c = false);

But javascript cannot do this:
newGame: function(a, b = 0)
ie and chrome will report an error, and ff will directly ignore it.
We can use the arguments read-only variable array to achieve this:
Copy the code The code is as follows:

function newGame(){
var a = arguments[0] ? arguments[0] : 1;
var b = arguments[1] ? arguments[1] : false;
//Function content
}
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