</br>
<a href="#1">1.eval</a>
<a href="#2">2.JSON字符串转JSON对象三种方式</a>
<a href="#3">3.instanceof</a>
<a href="#4">4.in</a>
</br>
</br>
<h5 id="1">1.eval</h5>
eval()运行字符串。
  var num = 100;  var mm = eval("num += 50");  console.log(num);//150
  console.log(mm);//150
Paste_Image.png
</br>
</br>
<h5 id="2">2.JSON字符串转JSON对象三种方式</h5>
<script>
    var json = '[{"name":"hehe","age":199},{"name":"heihei","age":999}]';    //1.使用json.parse,注意json闭合使用'',key使用"",严格按照格式避免错误
    var parseJSON = JSON.parse(json);    //2.使用Function创建一个函数将字符串作为代码运行,获得返回值json对象,注意创建好自调用()
    var funJSON = new Function("return "+json)();    //3.使用eval将字符串做代码执行获得json对象,注意()包裹,json字符串如果是一个{}对象需要加上()当成表达式执行。
    var evalJSON = eval("("+json+")");    console.log(parseJSON);    console.log(parseJSON);    console.log(parseJSON);</script>
Paste_Image.png
</br>
</br>
<h5 id="3">3.instanceof</h5>
instanceof运算符通过比对原型对象确定是否是其实例对象。
function A(){}var a = new A();console.log(a.__proto__ == A.prototype);  //trueconsole.log(a instanceof A);  //trueA.prototype = {};//改变原型console.log(a instanceof A);  //false</br>
</br>
<h5 id="4">4.in</h5>
in 运算符通过循环遍历对象属性来确定对象有无该值,值为undefined的属性结果都为false;
<script>
    function A(){}    var a = new A();    console.log(("haha" in a)); //false
    console.log((!!a.haha)); // 直接通过.语法测试  false
    console.log((!!a["haha"]));  // false
    a.haha;//定义 依旧为undefined
    console.log(("haha" in a)); // false
    a.haha = "haha";    console.log(("haha" in a)); //true</script></br>
</br>
</br>
作者:ThingLin
链接:https://www.jianshu.com/p/1950e5968e75
 
		 随时随地看视频
随时随地看视频 
				 
				 
				 
				