SoFunction
Updated on 2025-04-10

The first entry-level Javascript basics


5): Function definition:
Method 1: Normal definition
function square(x){
      return x*x;
}
Method 2: Function direct quantity definition
var square = function(x){  return x*x; }   //Recommended to use
Method 3: Construct parameters
var square = new Function("x","return x*x;");   //Low efficiency

6): Object:
If there is an object named cssrain, it has a high-height property.
Then we can quote it like this:
;
You can also use associative array definition: cssrain["height"];

Create an object:
Method 1:
var point = new Object();
 = 3;
 = 5;
Method 2: Use object to measure directly
var point = {x:3 , y:5 }
Of course json is OK.

In the context of characters, the toString() method will be called.
In a digital environment, the valueOf() method will be called.
In a Boolean environment, non-empty objects are true;

7): Array:
Regular array: Use non-negative integers as subscripts. image[0]
Associative array: characters are used as subscripts. For example: image["width"]
JS does not support multi-dimensional arrays, but arrays can be nested in arrays.

Create an array:
Method 1:
var a = new Array();
a[0] = “1”;
a[1] = 2;
a[2] = { x:1, y:3};
Method 2:
var  a  =  new Array(“1” , 2 , {x:1,y:3} );
Note: If only one parameter is passed; for example, var a = new Array(3);
Then it represents: a new array of 3 undefined elements.
Method 3: Use array direct quantity
var  a =["1" ,  2 , {x:1 , y :3 }]; //Note the brackets outside, not the braces.

8): null and undefined:
null means no value;
undefined: Use an undeclared variable, or use a declared variable but not assigned a value or use a property that does not exist.
undefined==null
If you want to distinguish:
You can use the ===   or typeof operator.

9. The common doubts that novices encounter:
var s =”you are right”;
var b = ((“ ”)-1 , );
Question: Is s an object or a string? Why does string have a method?
Answer: s is a string. The reason why there is a method is that the string type has a corresponding object class (String).
Similarly, numbers and booleans have corresponding Number  and Boolean classes.
Js will carry out corresponding packaging objects internally. The String object replaces the original string.

Summarize:
A brief introduction to some concepts (lexical structure) and data types (parts) in js.
Okay, that’s all for today, let’s continue tomorrow. ^_^.

I have read the notes for some members, and there are both good comments and bad comments. So I'll make a solemn statement:
This is the notes I took after reading the art of DOM programming, and understood JavaScript, and the authoritative guide to javascript.
I am not qualified and cannot write profound articles.
If you think your notes are not well written, you can not read my future. This article is a waste of your minutes.