(x中的“ __proto__”)可以返回false吗?

时间:2018-10-11 12:00:29

标签: javascript

我正在寻找使该函数返回true的param值

function mustBeTrue(param) {
  return param && !("__proto__" in param);
}

我解决这个问题的尝试如下: 我以为我可以从任何对象中删除属性__proto__并将其传递给param,但无法将其删除。

var obj = {foo: 'bar'};

delete obj.foo; //testing delete
console.log('foo' in obj);  //false
delete obj.__proto__
console.log('__proto__' in obj); //true Why ??

2 个答案:

答案 0 :(得分:1)

除了@ Pac0的答案:

实际上,'__proto__' in {}在某些浏览器(IE <11)中返回true

当前,所有现代浏览器都实现了__proto__属性,无法删除该属性,但将其标记为已弃用,因此情况可能会很快发生变化(或者可能不像现在的标准状态那样)。

来自MDN(重点是我):

  

使用__proto__是有争议的,因此不建议使用。它最初从未包含在EcmaScript语言规范中,但是现代的浏览器决定还是要实现它。直到最近,__proto__属性已经在ECMAScript 2015语言规范中针对Web浏览器进行了标准化,以确保兼容性,因此将来会受到支持已弃用,而推荐使用Object.getPrototypeOf/Reflect.getPrototypeOfObject.setPrototypeOf/Reflect.setPrototypeOf

答案 1 :(得分:1)

经过数小时的研究,实际上有可能通过将using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using System.Diagnostics; public class PythonCaller : MonoBehaviour { private string buffer = ""; private Process pythonML; // Use this for initialization void Start () { this.pythonML = new System.Diagnostics.Process(); this.pythonML.StartInfo.FileName = "/bin/bash"; this.pythonML.StartInfo.Arguments = "{{path to python executable}}/python {{path to python script}}/test.py"; this.pythonML.StartInfo.UseShellExecute = false; this.pythonML.StartInfo.RedirectStandardInput = true; this.pythonML.StartInfo.RedirectStandardOutput = true; this.pythonML.StartInfo.RedirectStandardError = true; this.pythonML.Start(); this.pythonML.BeginOutputReadLine(); this.pythonML.OutputDataReceived += new DataReceivedEventHandler((sender, e) => { UnityEngine.Debug.Log(e.Data); }); this.pythonML.WaitForExit(); } // Update is called once per frame void Update () { foreach (char c in Input.inputString) { if (c == '\b') { if (this.buffer.Length > 0) { this.buffer = this.buffer.Substring(0, this.buffer.Length - 1); } } else if (c == '\n' || c == '\r') { try { UnityEngine.Debug.Log(this.buffer); this.pythonML.StandardInput.WriteLine(this.buffer); } catch (IOException e) { UnityEngine.Debug.Log(e.Message); } this.buffer = ""; } else { this.buffer += c; } } } 设置为null来从对象中删除。

enter image description here