javascript vs PHP function
this is substr()
function in PHP and JavaScript
JavaScript syntax:
string.substr(start,length)
PHP syntax:
substr(string,start,length)
in PHP to cre开发者_JAVA百科ate this function, we do this
PHP:
function substr(string,start,length){}
how to create function like JavaScript syntax above in JavaScript?
JavaScript:
function substr(start,length){}
this javascript function will not exactly the same as it's syntax, and it creates a function like PHP does. i dont know where to put string
in this js function in order to be similar to the original one...
In JavaScript, the substr function would be defined like this:
String.prototype.substr = function (start, length) {
...
}
You simply add functions to the String.prototype object to add functions to JavaScript string objects.
Javascript:
function substr(string,start,length){
return string.substr(start,length);
}
Update: If you just want to know how to create a non-OOP substr()
function:
function substr(str, start, len) {
return str.substr(start, len);
}
Original answer:
length === undefined
if you omit the argument.
If you want to give it a default value, you can use something like this:
if(length === undefined) { length = string.length; }
(this would set length
to the length of the passed string if omitted)
You can also access the number of arguments which have been actually passed via arguments.length
精彩评论