In javascript, what happens when you new() a function?
Possible Duplicate:
What is the 'new' keyword in JavaScript?
I'm learning about prototypes in Javascript and wondered what this code is doing. It's not like what I've run across in Java or C#:
function MyObject(Parameter)
{
this.testString = Parameter;
}
var objectRef = new MyObject( "myValue" );
What's going on with that new MyObject("value") bit? I understand that in javascript functions are objects, but I'm still wrapping my head around what's going on when you new() a function?
What its doing is the variable objectRef is creating a new object so everything in that function can be called on the variable affecting itself only. Let me give you a demonstration:
var cat = new MyObject("Im a cat");
var dog = new MyObject("Im a dog");
console.log(cat.testString);
// "Im a cat"
console.log(dog.testString);
// "Im a dog"
I hope that's of some help.
链接地址: http://www.djcxy.com/p/40856.html上一篇: 什么时候不*使用新的工作