>  기사  >  웹 프론트엔드  >  JavaScript는 이 객체를 생성한 Date 함수를 참조하는 속성 생성자를 반환합니다.

JavaScript는 이 객체를 생성한 Date 함수를 참조하는 속성 생성자를 반환합니다.

黄舟
黄舟원래의
2017-11-04 13:55:482227검색

정의 및 사용법

constructor 속성은 이 객체 를 생성한 Date 함수에 대한 참조를 반환합니다.

Syntax

object.constructor

Example

이 예에서는 생성자 속성을 사용하는 방법을 보여줍니다.

<script type="text/javascript">

var test=new Date();

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>

Output:

This is a Date

Example & Description

[네이티브 코드]는 다음 코드에서 이것이 맨 아래임을 의미합니다. 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는 이 객체를 생성한 Date 함수를 참조하는 속성 생성자를 반환합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.