JavaScript functions, objects, and arrays

Source: Internet
Author: User

First, JavaScript functions

1. Define functions: The general syntax for functions is as follows

function function_name ([parameter [, ...]) {    statements;}
    • The definition of the function starting from the keyword functions
    • followed by the function name, which must start with a letter or underscore, followed by a string of letters, numbers, $ symbols, or underscores
    • Must have parentheses, the arguments in parentheses are optional, and multiple parameters are separated by commas (,)
    • Write function code in enclosed curly braces

2. Parameter array (arguments): The parameter array makes it more flexible to handle variables with multiple parameters.

function Displayitems () {    for (j = 0; J < displayItems.arguments.length; + +J        )+ "<BR/ > ")}

3. Function return value: Returns the function result using the return statement.

document.write (Fixnames ("The", "DALLAS", "Cowboys"))function  fixnames () {    var s = "" For     (var i = 0; i < fixNames.arguments.length; + +I     )        {+ = Fixnames.arguments[i].charat (0). toUpperCase () +        fixnames.arguments[i]. substr (1). toLowerCase () + ""    }    return s.substr (0,s.length-1)}

The above code capitalizes the first word of the input character, the remaining letters are lowercase, and the characters are stitched back.

If we do not want the characters to be stitched together, but instead return a separate single character, you can make the function return an array:

Words = Fixnames ("The", "DALLAS", "Cowboys")for (j = 0; J < words.length; ++ + "<br/>" c3>)function  fixnames () {  varnew  Array ()  for (j = 0; J < fi XNames.arguments.length; + +J    )= Fixnames.arguments[j].charat (0). toUpperCase () +    fixnames.arguments[j].substr ( 1). toLowerCase ()  return  s}
Second, JavaScript objects

1. Unlike other object-oriented programming languages, JavaScript is a prototype-based object-oriented language, not a strict object-oriented language, but an object-based language. There is no traditional class in JavaScript to instantiate objects, but for ease of understanding, we can introduce conceptual classes. objects can be generated by a special function called a constructor, and we can think of this process as a declaration of a class. Let's take a look at a concrete example: how the user class is constructed.

functionUser (forename,username,password) { This. forename =forename This. Username =username This. Password =Password This. Showuser =function() {document.write ("Forename:" + This. forename + "<br/>") document.write ("UserName:" + This. Username + "<br/>") document.write ("Password:" + This. Password + "<br/>")    }}

This function differs from the general function in two points:

    1. It refers to an object named this, which references the created instance when the program creates a user instance by running the function.
    2. Create a new function within the function named Showuser

We can think of this function as a class: the User class

2. Creating objects: Creating objects with the New keyword

// create an object directly using the New keyword New User ("Wolfgang", "W.a.mozart", "composer")// or create an empty object, and then assign the value new= " Wolfgang "=" W.a.mozart "=" Composer "// In addition, you can add a new property for the created object detail.greeting =" Hello "

3. Access object: JavaScript also uses a period (.) To access the properties and methods of an object,

4.prototype (prototype) Keywords: properties and methods defined using prototype, similar to static properties and static methods in C + +. All that is instantiated by this

JavaScript functions, objects, and arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.