JavaScript は、このオブジェクトを作成した Date 関数を参照するプロパティ コンストラクターを返します。

黄舟
リリース: 2017-11-04 13:55:48
オリジナル
2256 人が閲覧しました

定義と使用法

コンストラクター プロパティは、この オブジェクトを作成した Date 関数への参照を返します。

構文

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>
ログイン後にコピー

出力:

This is a Date
ログイン後にコピー

例と説明

次のコードの [ネイティブ コード] は、これが一番下であることを意味します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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!