SoFunction
Updated on 2025-04-13

Study Day 3

For a lot of confused knowledge in the past, it is necessary to sort out it out again: start with the most basic variables.

1.. variable variable

a. Global variable Global Variable and Local Variable Private Variable

There is a difference between adding var and not adding var
--》If you declare a variable without the keyword var, then this is a global variable and any subfunction can be accessed, even if the curly braces pop out, it can be accessed;
--》If the keyword var is used, the variable can be accessed from the "curly braces {}" where it is located, such as:


function foo(){
  i=8                    //Global Variable
  alert(i)
 }

function foo2(){
var i=88 //Variable i can be accessed under this curly brace
  alert(i);
  child()
  function child(){alert(i)}
 } 
foo();foo2()
alert(i) //The i here is still 8

It is worth noting that function is an object in the compile period and must be executed or instantiated to allocate this variable in memory.
Global variables are used to starting with _; it is best to use all variables with caution, you know very well when this variable will change!
For more information, please refer to the js manual:
"Although it is not safe, it is legal JScript syntax to ignore the var keyword in the declaration statement. At this time, the JScript interpreter gives the variable a visibility of the global scope. When a variable is declared at the process level, it cannot be used in the global scope; in this case, the variable declaration must use the var keyword."


b. Data type of variable the types of variable

Jscript has three main data types, two composite data types and two special data types.

The main (basic) data types are:

String
Value
Boole
The composite (reference) data type is:

Object
Array
The special data types are:

Null 
Undefined 
Here is a brief description of various object types in JavaScript:
Native Object: The JavaScript language provides objects that do not depend on the execution host. Some of them are built-in objects, such as Global and Math; some are created and used in the script running environment, such as: Array, Boolean, Date, Function, Number, Object, RegExp, Error.
Build-in Object: The built-in objects provided by the JavaScript language that do not rely on the execution host, such as: Global and Math; the built-in objects are all Native Objects.
Host Object: Any object that depends on the host environment provided by the JavaScript language. All non-Native Object objects are host objects, such as: window in IE, wscript instances in WScript, and any user-created classes.



****How ​​to check object types? *******
   ()
The typeof operator returns the type information as a string. typeof There are six possibilities for return value: “number,” ”string,” ”boolean,” ”object,” ”function,” and “undefined.”

    2.   val instanceof Array
Returns a Boolean value indicating whether the object is an instance of a specific class.
For example, when checking array or date types (in fact, any type is allowed, see example), instance of + class name (without quotes), example:

function foo(){}
var f = new foo();
alert(f instanceof foo2) //false

 
The usage of constructor feels the same as the usage of instance, except that it does not return the boolean value.

x = new String("Hi");
if ( == String)
// Processing (condition is true).

********* What type is ***var i={}? ****
Answer: object type is equivalent to var i = new Object

Object objects are the carriers of all objects, so I think of the parent class.

Object object is very simple,
It only has two properties and two methods
These two properties are:
prototype 
constructor 
These two functions are:
toString() 
valueOf() 

So how to textualize var obj = new MyObject()? In fact, it is also very simple. The textual definition of obj is as follows:

 

 var obj = 
     {
         Properties1 : 1, Properties2 : '2', Properties3 : [3],
         Method1 : function(){ return this.Properties1 + this.Properties3[0];},
         Method2 : function(){ return this.Preperties2; }
     };



The syntax of class instance textual definition is to use a pair of "{}" to represent the class, which means that "{}" is completely equivalent to "new Object()". Then, press "key:value" in "{}" to organize the properties and methods. The key can be any combination of characters of [A-Za-z0-9_], and even the beginning of the number is legal @_@. The value is any legal textized JavaScript data. Finally, each key-value pair is separated by "," .
Usually used for JSON exchange data.


********** Two meanings of undefined*******
Keyword Attributes
Declaring a variable but not assigning a value is the first case;
Without a declaration at all, a variable will be run to participate in the operation, and its data type is the second case;
The two names have different meanings, so it is recommended to change the name of the next version.


var declared;                                                                                                                           �
if (declared == undefined)                                                                                                                       �
  ("declared has not been given a value.");

if (typeOf(notDeclared) == "undefined")
  ("notDeclared has not been defined.");