简体   繁体   English

为什么在javascript中我不能在这个函数中创建对象?

[英]why in javascript i cant create object in this function?

i have a problem with creating object inside my function i know its a void function but i don't need to return any data我在函数内创建对象时遇到问题我知道它是一个 void 函数,但我不需要返回任何数据

let btnConnect = document.getElementById('connect')
let btnConnectedDrive = document.getElementById('ConnectedDrive')

function createPeer() {
    let myPeer = new Peer()
    console.log(myPeer.id)
    let socket = new WebSocket('ws://' + window.location.host + '/ws/myapp/room/' + game_id + '/');
    socket.onopen = () => socket.send(JSON.stringify(
        {
            'username': user,
            'PeerId': myPeer.id
        }
    ),)

}

btnConnect.onclick = () => createPeer();

now when i log myPeer.id its null and same on the server what do you think solution is ?现在,当我在服务器上记录 myPeer.id 它的null和相同时,您认为解决方案是什么? hope you have a very nice time .希望你过得愉快。

See the documentation :请参阅文档

Other peers can connect to this peer using the provided ID.其他对等点可以使用提供的 ID 连接到该对等点。 If no ID is given, one will be generated by the brokering server .如果没有给出 ID,将由代理服务器生成一个

And the example:还有这个例子:

 peer.on('open', function(id) { console.log('My peer ID is: ' + id); });

It doesn't have an ID because you haven't waited for it to connect to the brokering server and be issued with one.它没有 ID,因为您没有等待它连接到代理服务器并获得 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能使用对内部函数的引用在Javascript中创建对象? - Why cant I create object in Javascript with references to internal functions? 为什么我不能迭代这个JavaScript对象? - Why cant I iterate over this JavaScript object? 为什么我不能将此变量传递给javascript函数? - why cant I pass this variable to the javascript function? 为什么我不能过滤我的javascript / jquery对象 - Why cant I filter my javascript/jquery object 为什么我无法推入数组并在javascript函数参数中使用它? - Why cant I push inside an array and use it in a function argument in javascript? 为什么我不能使用“ this”将对象中的对象变量分配给JavaScript中该对象中的另一个变量 - Why I cant use “this” to assign a object variable from a object to another variable in that object in JavaScript javascript adodb流错误无法创建对象 - javascript adodb stream error cant create object 为什么我的主播不能发射这个 javascript object? - Why cant my anchor fire this javascript object? 为什么我不能通过 Javascript 中的函数构造函数创建纯 object? - Why I cannot create a pure object via a function-constructor in Javascript? 为什么不能在该对象文字中创建与全局函数相等的方法? (javascript) - Why can't I create a method equal to a global function within this object literal? (javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM