[关闭]
@zhouyy 2018-02-28T08:15:44.000000Z 字数 2974 阅读 519

function

js


  1. var x=2;
  2. A();
  3. var A=function(){
  4. var x=5;
  5. B();
  6. }
  7. var B=function(){
  8. console.log(x);
  9. }

Result: 2

Even though B is called within A and A has its own x, what actually matters as far as resolving where x is coming from is its outer reference. And B is defined within the global scope. Therefore, the outer reference of function B is the global scope, not function A. What this means is that it does not matter where a particular function is executed. What matters as far as resolving scope of a variable is concerned is where that function is physically defined. Function B is physically defined inside the global scope. Therefore a reference of x inside function B Is going to look inside function B first. If it doesn't find it it's going to use its outer reference to look for it there and its outer reference is the global scope. Let's jump to the code editor and see an example of this.

null undefined 区别

JavaScript defines seven built-in types, one object and six primitives. Now object type is a collection of name value pairs. It is nothing more than that in JavaScript. A primitive type can only contain a single, immutable value. And remember thar immutable means that once you set it, it cannot be changed. You could create new values based on this values, but you cannot change the original value anymore. Now, there's a particular interesting type called undefined among the primitives, and undefined means variable memory has been allocated, but not value has ever been explicitly set yet. And it's important to distinguish between that and a variable that has never been defined, in which case if you try to use it, you're going to get a reference error.

function factory

  1. function multiply(x,y) {
  2. return x*y;
  3. // body...
  4. }
  5. multiply.version="v1.0.0";
  6. console.log(multiply.version);
  7. //function factory
  8. function MakeMultiplier(multiplier){
  9. var myFunc=function(x){
  10. return multiplier*x;
  11. }
  12. return myFunc;
  13. }
  14. var multiplyBy3=MakeMultiplier(3);
  15. console.log(multiplyBy3(10));
  16. //passing function as argument
  17. function doOperation(x, operation){
  18. return operation(x);
  19. }
  20. var result=doOperation(50,multiplyBy3);
  21. console.log(result);

constructors and prototype

  1. //Function constructors
  2. function Circle(argument) {
  3. this.radius=argument;
  4. }
  5. // prototype
  6. Circle.prototype.getArea= function() {
  7. return Math.PI*Math.pow(this.radius,2);
  8. };
  9. Circle();
  10. var myCircle=new Circle(10);
  11. console.log(myCircle.getArea());

Object literals and "this"

  1. // Object literals and "this"
  2. var literalCircle={
  3. radius:10,
  4. getArea:function () {
  5. var self=this;
  6. console.log(this);
  7. function increaseRadius(){
  8. self.radius=self.radius*3;
  9. }
  10. increaseRadius();
  11. return Math.PI*Math.pow(this.radius,2)
  12. }
  13. };
  14. console.log(literalCircle.getArea());

闭包

子包可以一层层链式访问父包

Fake namespace

js1.js

  1. var namesapce1={};
  2. namespace1.name="zh";
  3. namespace1.sayhi=function(name){
  4. console.log("hi"+namespace1.name);
  5. }

js2.js

  1. var namesapce2={};
  2. namespace2.name="zh2";
  3. namespace2.sayhi=function(name){
  4. console.log("hi"+namespace2.name);
  5. }

html

  1. namespace1.sayhi();
  2. namespace2.sayhi();

Immediately Invoked Function

Immediately Invoked Function Expressions are usually used to place code into its own execution context not to conflict with the global scope.

  1. (function(name){
  2. console.log("hello"+name);
  3. })("YY");
  4. (function(window){
  5. var namespace1={};
  6. namespace1.name="zh";
  7. namespace1.sayhi=function(name){
  8. console.log("hi"+namespace1.name);
  9. window.namespace1=namespace1;
  10. }
  11. }
  12. )(window);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注