Q: What is the difference between arrays and objects?
A: Array is a type of object, so if we are going to draw out the circle diagram array would be inside the object circle. When an object is created, it will automatically inherit the object prototype functions. This is where arrays and objects start to show their differences. A normal object will only inherit the object prototype functions, whereas arrays will also inherit the Array prototype functions in addition to the object prototype functions.
Q: What would happen if a variable were passed into the constructor of a function, and then the variable is changed, what will happen?
function test(a){
this.a = a;
}
var b = ['1', '2', '3'];
var something = new test(b);
b.push('4');
console.log(b) //output 1
console.log(something.a) //output 2
沒有留言:
張貼留言