정의 및 사용법
생성자 속성은 이 객체를 생성한 배열 함수에 대한 참조를 반환합니다.
Syntax
object.constructor
Examples
예제 1
이 예에서는 생성자 속성을 사용하는 방법을 보여줍니다.
<script type="text/javascript">
var test=new Array();
if (test.constructor==Array)
{
document.write("This is an Array");
}
if (test.constructor==Boolean)
{
document.write("This is a Boolean");
}
if (test.constructor==Date)
{
document.write("This is a Date");
}
if (test.constructor==String)
{
document.write("This is a String");
}
</script>출력:
This is an Array
예제 2
이 예에서는 생성자 속성 :
<script type="text/javascript">
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
var bill=new employee("Bill Gates","Engineer",1985);
document.write(bill.constructor);
</script>출력:
function employee(name, job, born)
{this.name = name; this.job = job; this.born = born;}예제 및 설명
[네이티브 코드]는 이것이 JavaScript의 기본 내부 코드 구현이며 코드 세부 정보를 표시할 수 없음을 의미합니다.
// 字符串:String()
var str = "张三";
alert(str.constructor); // function String() { [native code] }
alert(str.constructor === String); // true
// 数组:Array()
var arr = [1, 2, 3];
alert(arr.constructor); // function Array() { [native code] }
alert(arr.constructor === Array); // true
// 数字:Number()
var num = 5;
alert(num.constructor); // function Number() { [native code] }
alert(num.constructor === Number); // true
// 自定义对象:Person()
function Person(){
this.name = "CodePlayer";
}
var p = new Person();
alert(p.constructor); // function Person(){ this.name = "CodePlayer"; }
alert(p.constructor === Person); // true
// JSON对象:Object()
var o = { "name" : "张三"};
alert(o.constructor); // function Object() { [native code] }
alert(o.constructor === Object); // true
// 自定义函数:Function()
function foo(){
alert("CodePlayer");
}
alert(foo.constructor); // function Function() { [native code] }
alert(foo.constructor === Function); // true
// 函数的原型:bar()
function bar(){
alert("CodePlayer");
}
alert(bar.prototype.constructor); // function bar(){ alert("CodePlayer"); }
alert(bar.prototype.constructor === bar); // true인스턴스 생성자의 프로토타입 개체를 노출하기 위해 예를 들어 플러그인을 작성하면 다른 사람이 인스턴스화한 개체를 가져오게 됩니다. 다른 사람이 개체를 확장하려는 경우에는 instance.constructor.prototype을 사용할 수 있습니다. . 프로토타입 객체 수정 또는 확장
위 내용은 JavaScript는 이 객체를 생성한 배열 함수에 대한 참조를 반환합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!