js basic study notes

javasrcipt Notes

New Template Bookmaker
In this example, you might expect the first alert to pop up "global" and the second to pop up "loacl". This expectation is understandable, because at the time of the first alert, myname is not declared, and the function must naturally look at the global variable myname at this time, but it does not actually work that way. The first alert will pop "undefined" because myname is treated as a local variable of the function (although it is declared later), and all variable declarations should be suspended at the top of the function. So, to avoid this confusion, it's best to predeclare all the variables you want to use.

The above code snippet might behave like this:

myname = "global"; // global variable
function func() {
var myname; // same as -> var myname = undefined;
alert(myname); // " undefined"
myname = "local";
alert(myname); // "local"}
func();

Avoiding eval()

If you use eval() in your current code, remember the mantra "eval() is the devil". This method accepts arbitrary strings and treats them as JavaScript code. There is no reason to use eval() when the code in question is known in advance (not determined at runtime). If the code is dynamically generated at runtime, there is a better way to achieve the same goal without using eval. For example, it is better and easier to access dynamic properties with square bracket notation:

    markdown 8 lines
// bad example
var property = "name";
alert(eval("obj." + property));

// better
var property = "name";
alert(obj[property]);
use new Function() The construct is like eval() and should be approached with care. This can be a powerful construct, but is often misused. If you absolutely must use eval(), you might consider using new Function() instead. There is a small potential benefit, since code evaluation in the new Function() is performed in the local function scope, so any variables defined by var in the code that are evaluated will not automatically become global variables. Another way to prevent automatic globals is to wrap the eval() call into an immediate function.

Consider the following example, where only un as a global variable pollutes the namespace.

    markdown line 19
console.log(typeof un); // "undefined"
console.log(typeof deux); // "undefined"
console.log(typeof trois); // "undefined"

var jsstring = "var un = 1 ; console.log(un);";
eval(jsstring); // logs "1"

jsstring = "var deux = 2;
new Function(jsstring)(); // logs "2"

jsstring = "var trois = 3; console. log(trois);";
(function () {
   eval(jsstring);
}()); // logs " 3"

console.log(typeof un); // number
console.log(typeof deux); // "undefined"
console.log(typeof trois); // "undefined"
another eval() and Function construct different It's eval() that can interfere with the scope chain, while Function() is more on the safe side. No matter where you execute Function(), it only sees the global scope. So it can well avoid local variable pollution. In the example below, eval() can access and modify variables in its outer scope, which Function cannot do (note that using Function is the same as new Function).

    markdown line 11
(function () {
   var local = 1;
   eval("local = 3; console.log(local)"); // logs "3"
   console.log(local); // logs "3"
}( ));



   Function("console.log(typeof local);")(); // logs undefined
}());
The call or apply method on Function.prototype and with etc.
It points to the object passed in the function parameter. At this point this will be explicitly set as the first parameter of the function call

    markdown line 9
function add(c,d){
return this.a + this.b + c + d
}

var o = {a:1,b :2}

add.call(o,3,4) //this in add() points to the incoming o object
add.apply(o,[3,4]) //this in add() points to the incoming The o object of
jQuery and other JavaScript frameworks
As you've already learned, jQuery uses the $ sign as a shorthand for jQuery.
What if other JavaScript frameworks also use the $ sign as a shorthand?
Some other JavaScript frameworks include: MooTools, Backbone, Sammy, Cappuccino, Knockout, JavaScript MVC, Google Web Toolkit, Google Closure, Ember, Batman, and Ext JS.
Some of these frameworks also use the $ sign as a shorthand (like jQuery), and if you're using two different frameworks that are using the same shorthand, it may cause the script to stop running.
The jQuery team took this into consideration and implemented the noConflict() method.
jQuery noConflict() method
The noConflict() method releases control of the $identifier so that other scripts can use it.
Example
Of course , you can still use jQuery by using the full name instead of the shorthand:

    markdown line 7
$.noConflict();
jQuery(document).ready(function(){
  jQuery("button").click(function(){
    jQuery("p").text("jQuery is still running!");
  });
});
prototype chain

    markdown line 21
ar a = {
  x: 10,
  calculate: function (z) {
    return this.x + this .y + z
  }
};

var b = {
  y: 20,
  __proto__: a
};

var c = {
  y: 30,
  __proto__: a
};

// Call the inherited method
b.calculate(30); // 60
c.calculate(40); // 80
looks very simple. b and c can use the calculate method defined in a, which is achieved by the prototype chain [prototype chain].

The principle is very simple: if the calculate method is not found in object b (that is, there is no calculate attribute in object b), then it will start looking along the prototype chain. If the calculate method is not found in the prototype of b, then the prototype of a will be found along the prototype chain, and the entire prototype chain will be traversed. Remember, once found, return the first property or method found. Therefore, the first property found becomes the inherited property. If the entire prototype chain is traversed and still not found, then undefined will be returned.

The execution context stack
property has 3
variable objects (variable object) is the data scope (scope of data) related to the execution context.
It is a special object associated with the context, used to store the variables (variables) and function declarations (function declarations) defined in the context.

Function expressions and function declarations

Scope chain
with

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326608596&siteId=291194637