xenophobia Posted July 27, 2008 Share Posted July 27, 2008 Let's say I got a class name: "Message" Now I had a function called: send(msg), which received 'msg' parameters as a instance of Message::object. So I will invoke the function like this: var msg_1 = new Message(); msg_1.content = "Hello World!"; send(msg_1); This work fine. So my question is, is there a shortcut, without declare the "msg_1" and directly passed in a object of the class into the parameter, which look something like this: send(new Message(){this.content="hello Word";}); I know it can be done in Java. I also seen some example of this in somewhere I forget in JS. Any idea? Link to comment https://forums.phpfreaks.com/topic/116809-oop-simple-question/ Share on other sites More sharing options...
corbin Posted July 27, 2008 Share Posted July 27, 2008 You could try: send((new Message()).content="Hello World!"); But I don't think that will work. If the object doesn't have to be a Message, you could always just do: send({"content": "Hello World!"}); Link to comment https://forums.phpfreaks.com/topic/116809-oop-simple-question/#findComment-600917 Share on other sites More sharing options...
KevinM1 Posted July 27, 2008 Share Posted July 27, 2008 Why not try something like: function Message(content) { this.content = content; } send(new Message("Hello World!")); Link to comment https://forums.phpfreaks.com/topic/116809-oop-simple-question/#findComment-600943 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.