JAVAScript Learning 2-Serialization, Escaping and Functions

Keywords: Javascript JSON encoding Python

Serialization:

Converting JS objects into JSON format

  • JSON.stringify(obj) serialization

  • JSON.parse(str) deserialization

var ls=[1,2,3,4];
var ls_st = JSON.stringify(ls);
var ls_ls = JSON.parse(ls_st);
document.write(ls_st);    //[1,2,3,4]
document.write(ls_ls);    //1,2,3,4


Transliteration:

Transliterate the Chinese character of the URL into encoding. Usually used to escape data and save it in local Cookies

  • encodeURI() = escape URL characters, translate Chinese characters and common symbols, do not turn! @$& *()+-=.

  • encodeURIComponent(). Escape URL characters, except for ~!*()-., everything else is changed.

  • DecdeURI () Restore escape characters in decodeURI

  • DecdeURIComponent () Restores the characters in the decodeURI component

  • escape() escaping strings

  • Unscape () Decodes escaped strings

  • URIError thrown by the encoding and decoding methods of URLs

var web_address='http://localhost:8080/test.html?user = administrator';

var web_encode=encodeURI(web_address);
document.write(web_encode);        //http://localhost:8080/test.html?user=%E7%AE%A1%E7%90%86%E5%91%98
var web_encode=decodeURI(web_encode);
document.write('<br>');
document.write(web_encode);            //http://localhost:8080/test.html?user=administrator
document.write('<br>');
var web_en_c=encodeURIComponent(web_address);      //The original address encode
        //http%3A%2F%2Flocalhost%3A8080%2Ftest.html%3Fuser%3D%E7%AE%A1%E7%90%86%E5%91%98
document.write('<br>');
var web_ene_c=encodeURIComponent(web_encode);       //encodeURI,encode, encode the% number as well.
        //http%3A%2F%2Flocalhost%3A8080%2Ftest.html%3Fuser%3D%25E7%25AE%25A1%25E7%2590%2586%25E5%2591%2598      
document.write('<br>');
var web_de_c=decodeURIComponent(web_en_c);        
        //http://localhost:8080/test.html?user=administrator
document.write('<br>');
var web_dee_c=decodeURIComponent(web_ene_c);   //Decode layer, Chinese characters need to decode/decodeURIC again can also be
        //http://localhost:8080/test.html?user=%E7%AE%A1%E7%90%86%E5%91%98


eval and exec:

eval(): Execute character expressions or code blocks.

Execute the expression to return the result.

Executing a block of code returns the result of the last variable with a = sign, and the variable operation of the block of code can change the variable value of the external code.

var a;
var multiple='var a=1;a=a+1;var b=1;b=b+6;'
var result=eval(multiple);
document.write(result);    // 7
document.write(a);         // 2


Scope:

With functions as scopes, variables within functions are declared before they are called (variables have no values, values are undefined)

Scope chain: The way to find variable values is to find yourself first-then father-then grandpa.

//Scope chain, subfunction has a variable

var a='grand';
function func(){
  var a='parent';
  function sub(){
    var a='son';
    document.write(a)
  }
  return sub;
}
var b=func()
b()                //son
//Scope chain example, where variables are called before a subfunction runs
var a='grand';
function func(){
  var a='parent';
  function sub(){
    document.write(a)
  }
  var a='son';
  return sub;    //Before the returned function
}
var b=func()
b()            //son
//Local variables in a function, declare variables first and var b when invoked
function func(){
  document.write(b);
  var b='s'            //b Declares after invocation, but the program does not report errors
}
func();        // undefined


Object this: self similar to python class

Creating an object with new allows you to call properties like python classes and create different instances

function Foo(n){
  this.name=n;
  this.sayname=function (){
  document.write(this.name)
  }
}
var f = new Foo('david');
document.write(f.name);        //daivd

var f1 = new Foo('sam');
document.write(f1.name);        // sam

f.sayname();                    //Use subfunctions as class method calls
f1.sayname();

Prototype prototype, which can add attributes to objects

function Foo(n){
  this.name=n;
}
var f = new Foo('david');
Foo.prototype.age=19;
document.write(f.age)        //19



Function:

Basic functions

function func(){
  var a=1;
  document.write(a);
}

Anonymous function: No function name. Usually written in other functions or methods

//Example 1
function Foo(n){
  this.name=n;
  this.action=function(){document.write('papapa');}
}
var f = new Foo('david');
f.action()
//Example two
var i=1;
setInterval(function(){i++;alert(i);},2000);

3. Self-executing function

Run directly without calling

(function test(n){
  for (var i=1;i<10;i++){
    document.write(i,':',n+i,'<br>');
  }
})(3)

4. Closure function: occupy large memory and use as little as possible

The problem with ordinary functions: The function's domain is the function itself. When the second call is made, the value of the last operation cannot be remembered, and the variables in the function are retrieved.

Closure Function Principle: Since func() is executed, func() objects are stored in addvariable, addvariable does not disappear, memory does not reclaim func().

The scope of count is in func(), and count+=1 also changes the value of varcount, so it implements accumulation.

var add=(function func(){
  var count=0;
  return function(){count+=1; return count;}
})();
document.write(add())
document.write(add())
document.write(add())

Interpretation of function implementation:

1. var add, declare variable add, scope-global,

2. (func) () is a self-executing function that executes func, so add=function(){count+=1;return count;}

3. Run add (), that is, run the anonymous function function() and change the value of count, count=2

4. The point is that the memory object saved by the add variable will continue to take effect, that is, the count scope will continue to exist.

So, run add () again, that is, run the anonymous function function() again, count plus 1, count=3


Posted by AndrewBacca on Fri, 09 Aug 2019 01:39:55 -0700