JavaScript typeof id=== ' string ' document.getElementById (ID): ID explanation [Original]_ Basics

Source: Internet
Author: User

In general, to understand this code, you need to master the following two functions

One is typeof, and one is the ternary operator.

Read the above article and then look at the following is better to understand

Generally used function code

function $ (ID) {return typeof id=== ' string ' document.getElementById (ID): ID;}
var Getby = function (ID) {return
      "string" = = typeof ID? document.getElementById (ID): ID;
    

function is the same.

The role of this function is to use the ID selector can be abbreviated, as far as the meaning of the judgement is that the incoming parameter is a string to return document.getElementById (ID), the other returns the parameter itself, this is just a page DOM elements of a primitive version of the function, is not perfect yet.

1, this is a section of JavaScript code
$ is an indicator. You can also use GetID and so on, if you use jquery on the page is not recommended to use $, because jquery defaults to use is $
? : Is an operator
Return "string" = = typeof ID? document.getElementById (ID): ID;
Can also be written as

if ("string" = = typeof id) return
  document.getElementById (ID);
Else
  

Some people feel that the above code, why not use {}, because the code is relatively short only one line is not required {}

As shown below

if ("string" = = typeof id)   {return
  document.getElementById (ID);
  }
else {return
  ID; 
   }

Specific can refer to this article: http://www.jb51.net/article/50197.htm

2, the expression of 1? Expression 2: Expression 3

This is a formula; his operation is: first, the expression 1, if true, the expression of the formula 2 of the operation, or the whole equation is the expression of the value of 3 of the operation results, do not understand the friend can refer to this article: http://www.jb51.net/article/64237.htm

In your case, expression 1 is "string" = = typeof ID, function is to determine whether the data type of ID is string, = = is the operator to determine whether equality
Expression 2 is document.getElementById (ID) An HTML element that gets the value of the ID as the parameter ID
Expression 3 is the ID of this variable (in general, this variable is mostly object), if the object is not document.getElementById, can be used directly.

Cloud Habitat Community Original collation, reprint please indicate the source

Related Article

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.