什么是';新';JavaScript中的关键字?
JavaScript中的什么是';新';JavaScript中的关键字?,javascript,new-operator,Javascript,New Operator,JavaScript中的new关键字在第一次遇到时可能会非常混乱,因为人们倾向于认为JavaScript不是一种面向对象的编程语言 这是什么 它解决了什么问题 什么时候合适,什么时候不合适 所以这可能不是为了创造 对象实例 正是为了这个。您可以这样定义函数构造函数: function Person(name) { this.name = name; } var john = new Person('John'); 然而,ECMAScript的额外好处是,您可以使用.prototy
new
关键字在第一次遇到时可能会非常混乱,因为人们倾向于认为JavaScript不是一种面向对象的编程语言
- 这是什么
- 它解决了什么问题
- 什么时候合适,什么时候不合适
function Person(name) {
this.name = name;
}
var john = new Person('John');
然而,ECMAScript的额外好处是,您可以使用.prototype
属性进行扩展,因此我们可以执行以下操作
Person.prototype.getName = function() { return this.name; }
从这个构造函数创建的所有对象现在都将有一个
getName
,因为它们可以访问原型链。JavaScript是一种面向对象的编程语言,它完全用于创建实例。它是基于原型的,而不是基于类的,但这并不意味着它不是面向对象的。Javascript是一种支持面向对象编程范式的动态编程语言,它用于创建对象的新实例
对象不需要类-Javascript是一种语言。关键字
new
使用函数作为构造函数创建对象实例。例如:
var Foo = function() {};
Foo.prototype.bar = 'bar';
var foo = new Foo();
foo instanceof Foo; // true
实例继承自构造函数的原型。所以给出上面的例子
foo.bar; // 'bar'
new
关键字用于创建新的对象实例。是的,javascript是一种动态编程语言,支持面向对象编程范式。关于对象命名的约定是,对于应该由new关键字实例化的对象,始终使用大写字母
obj = new Element();
假设您有此函数:
var Foo = function(){
this.A = 1;
this.B = 2;
};
如果将其作为独立函数调用,如下所示:
Foo();
执行此函数将向窗口对象添加两个属性(A
和B
)。它将其添加到窗口
,因为窗口
是这样执行函数时调用函数的对象,而函数中的此
是调用函数的对象。至少用Javascript
现在,用new
这样称呼它:
var bar = new Foo();
将new
添加到函数调用时会发生的情况是创建了一个新对象(只是var bar=new object()
),并且函数中的this
指向刚刚创建的新对象,而不是指向调用函数的对象。所以bar
现在是一个具有A
和B
属性的对象。任何函数都可以是构造函数,但它并不总是有意义的 它可以做5件事:
它会创建一个新对象。这个对象的类型就是简单的对象
它将此新对象的内部、不可访问的[[prototype]](即\uuuu proto\uuuu)属性设置为构造函数的外部、可访问的原型对象(每个函数对象自动具有prototype属性)
它使此
变量指向新创建的对象
只要提到这个
,它就会使用新创建的对象执行构造函数
它返回新创建的对象,除非构造函数返回非空的对象引用。在本例中,将返回该对象引用
注意:构造函数是指new
关键字后面的函数,如中所示
new ConstructorFunction(arg1, arg2)
完成此操作后,如果请求新对象的未定义属性,脚本将检查对象的[[prototype]]对象以获取该属性。这就是如何在JavaScript中获得类似于传统类继承的内容
最困难的是第二点。每个对象(包括函数)都有一个名为[[prototype]]的内部属性。只能在对象创建时使用new、object.create或基于文本(函数默认为Function.prototype、数字为Number.prototype等)设置。只能使用Object.getPrototypeOf(someObject)读取它。没有其他方法可以设置或读取此值
除了隐藏的[[prototype]]属性外,函数还有一个名为prototype的属性,您可以访问和修改该属性,为创建的对象提供继承的属性和方法
以下是一个例子:
ObjMaker = function() {this.a = 'first';};
// ObjMaker is just a function, there's nothing special about it that makes
// it a constructor.
ObjMaker.prototype.b = 'second';
// like all functions, ObjMaker has an accessible prototype property that
// we can alter. I just added a property called 'b' to it. Like
// all objects, ObjMaker also has an inaccessible [[prototype]] property
// that we can't do anything with
obj1 = new ObjMaker();
// 3 things just happened.
// A new, empty object was created called obj1. At first obj1 was the same
// as {}. The [[prototype]] property of obj1 was then set to the current
// object value of the ObjMaker.prototype (if ObjMaker.prototype is later
// assigned a new object value, obj1's [[prototype]] will not change, but you
// can alter the properties of ObjMaker.prototype to add to both the
// prototype and [[prototype]]). The ObjMaker function was executed, with
// obj1 in place of this... so obj1.a was set to 'first'.
obj1.a;
// returns 'first'
obj1.b;
// obj1 doesn't have a property called 'b', so JavaScript checks
// its [[prototype]]. Its [[prototype]] is the same as ObjMaker.prototype
// ObjMaker.prototype has a property called 'b' with value 'second'
// returns 'second'
这类似于类继承,因为现在,您使用new ObjMaker()
创建的任何对象似乎也继承了“b”属性
如果您想要类似子类的东西,那么可以执行以下操作:
SubObjMaker = function () {};
SubObjMaker.prototype = new ObjMaker(); // note: this pattern is deprecated!
// Because we used 'new', the [[prototype]] property of SubObjMaker.prototype
// is now set to the object value of ObjMaker.prototype.
// The modern way to do this is with Object.create(), which was added in ECMAScript 5:
// SubObjMaker.prototype = Object.create(ObjMaker.prototype);
SubObjMaker.prototype.c = 'third';
obj2 = new SubObjMaker();
// [[prototype]] property of obj2 is now set to SubObjMaker.prototype
// Remember that the [[prototype]] property of SubObjMaker.prototype
// is ObjMaker.prototype. So now obj2 has a prototype chain!
// obj2 ---> SubObjMaker.prototype ---> ObjMaker.prototype
obj2.c;
// returns 'third', from SubObjMaker.prototype
obj2.b;
// returns 'second', from ObjMaker.prototype
obj2.a;
// returns 'first', from SubObjMaker.prototype, because SubObjMaker.prototype
// was created with the ObjMaker function, which assigned a for us
在最终找到之前,我读了很多关于这个主题的废话,这些废话用漂亮的图表解释得很好。除了Daniel Howard的答案,以下是新的所做的(或者至少看起来是这样做的):
当
相当于
var obj = new A(1, 2);
每个si的JavaScript可能因平台而异,因为它始终是原始规范EcmaScript的实现
在任何情况下,独立于实现,所有遵循EcmaScript规范的JavaScript实现都将为您提供一种面向对象的语言。根据ES标准:
ECMAScript是一种面向对象的编程语言
执行计算和操纵计算对象
在主机环境中
现在我们已经同意JavaScript是EcmaScript的一个实现,因此它是一种面向对象的语言。在任何面向对象的语言中,new
操作的定义是这样的:这样的关键字用于从特定类型的类(包括匿名类型,在类似C#的情况下)创建对象实例
在EcmaScript中,我们不使用类,正如您可以从
var obj = New(A, 1, 2);
var obj = new A(1, 2);
var func1 = function (x) { this.x = x; } // used with 'new' only
var func2 = function (x) { var z={}; z.x = x; return z; } // used both ways
func1.prototype.y = 11;
func2.prototype.y = 12;
A1 = new func1(1); // has A1.x AND A1.y
A2 = func1(1); // undefined ('this' refers to 'window')
B1 = new func2(2); // has B1.x ONLY
B2 = func2(2); // has B2.x ONLY
function Foo() {
return this;
}
var a = Foo(); //returns window object
var b = new Foo(); //returns empty object of foo
a instanceof Window; // true
a instanceof Foo; // false
b instanceof Window; // false
b instanceof Foo; // true
var Foo = function(){
this.A = 1;
this.B = 2;
};
console.log(Foo()); //prints undefined
console.log(window.A); //prints 1
var Foo = function(){
this.A = 1;
this.B = 2;
};
var bar = new Foo();
console.log(bar()); //illegal isn't pointing to a function but an object
console.log(bar.A); //prints 1
var Foo = function(){
this.A = 1;
this.B = 2;
return {C:20,D:30};
};
var bar = new Foo();
console.log(bar.C);//prints 20
console.log(bar.A); //prints undefined. bar is not pointing to the object which got created due to new keyword.
" Every object (including functions) has this internal property called [[prototype]]"
const a = { name: "something" };
console.log(a.prototype); // undefined because it is not directly accessible
const b = function () {
console.log("somethign");};
console.log(b.prototype); // returns b {}
console.log(a.__proto__); // returns {}
console.log(b.__proto__); // returns [Function]
function CreateObject(name,age){
this.name=name;
this.age =age
}
const me=new CreateObject("yilmaz","21")
function CreateObject(name,age){
this.name=name;
this.age =age;
const myJob="developer"
}
const me= {name:"yilmaz",age:21} // there is no myJob key
CreateObject.prototype.myActions=function(){ //define something}
const a = new Number(5);
console.log(a); // [Number: 5]
console.log(typeof a); // object
const b = 5;
console.log(a === b);//false
function CreateObj(value1, value2) {
const newObj = {};
newObj.property1 = value1;
newObj.property2 = value2;
return newObj;
}
var obj = CreateObj(10,20);
obj.__proto__ === Object.prototype; // true
Object.getPrototypeOf(obj) === Object.prototype // true
function CreateObj(value1, value2) {
this.property1 = value1;
this.property2 = value2;
}
var obj = new CreateObj(10,20);
obj.__proto__ === CreateObj.prototype // true
Object.getPrototypeOf(obj) == CreateObj.prototype // true
function CreateObj(value1, value2) {
var isWindowObj = this === window;
console.log("Is Pointing to Window Object", isWindowObj);
this.property1 = value1;
this.property2 = value2;
}
var obj = new CreateObj(10,20); // Is Pointing to Window Object false
var obj = CreateObj(10,20); // Is Pointing to Window Object true
window.property1; // 10
window.property2; // 20