javascript study notes 1: javascript prototype + object literal, etc.

1. When using for-in to traverse objects (for-in is best not to traverse arrays, traversing arrays generally use for-each), it is best to filter out the interference of the prototype chain of the object
if(Object.prototype.clone === 'undefined'){
		Object.prototype.clone = function(){}
	}

	for(var key in MyObject){
		if(MyObject.hasOwnProperty(key)){//代表是有原型链的,原型链中的有一些对象或者属性可能会与我们正在遍历的对象的属性发生冲突,故而应该过滤掉原型链。

		}
	}

2. To increase the properties (methods) of the prototype, you need to use methods such as: Object.prototype.Properties/Methods = .../function(){}.


3. Implicit global variables and explicit global variables: Implicit global variables are global variables declared in functions (funtion); explicit global variables are not global variables declared in functions.

3. Avoid implicit type conversion in the code: when it is judged that "false == 0", it returns true. This situation is not logical. At this time, we have to use "false === 0" to avoid this type of implicit conversion. The style has been converted.

4. Never use eval(): This function can execute any string as a javascript code fragment. When the code to be discussed is pre-written, there is no reason to use eval().
  If the code is dynamically generated at runtime, there are other ways to achieve the corresponding function.

 例子:
		var a = eval('1234+567');//结果是1810
		var b = '1234+567';//结果是1234+567
		alert(a);

5.parseInt(): This method will parse a string and return the value in it. When the string is of the type "09", using parseInt('09') will return 0, because the string starting with 0 will It is parsed as octal.
  In this case, we need to use another parameter to constrain this method, like this parseInt('09',10) indicates that the string is parsed in decimal when parsing the string, and the returned result is 9; of
  course If the string is all numeric values, you can also use the Number('09') method to get the result of 9, but if the string like "009 Hello" is converted using the Number() method, the result returned is NaN At this time, it
  is better to use the parseInt() method.

6. "Object literal" in javascript:

  var Person = function(name){
	this.name = name,
	this.say = function(){
		return 'I am '+this.name;
	}
  }

When we use new to use this constructor, the following things actually happen internally:

 var Person = function(){
	  //创建了一个“空”对象,此对象的引用指向了Person 这个对象
	  var this = {};
	  //想this这个“空”对象添加属性和方法
	  this.name =  name,
	  this.say = function(){
		  return 'I am '+this.name;
	  }

  }

  In fact, it is inaccurate that the this object mentioned above is an empty object, because this also inherits all the members in the prototype of Person. Therefore, it is more like the following statement:
    this = Object.create(Person.prototype)
  And in the constructor, we can return any parameter, as long as the parameter is an object.

7. Reusable methods in javascript should be placed in the first prototype.


Guess you like

Origin blog.csdn.net/nanxiaotiantian/article/details/20216143