検索
ホームページウェブフロントエンドjsチュートリアルある人が初めて javascript_javascript スキルを学んだときに書いた学習メモ

コードをコピー コードは次のとおりです:

/*
* A JavaScript object is a collection of properties (methods)
* In this language, if the variable name or method name does not comply with the declaration specification,
* must be used Square brackets "[]" refer to it
*
*/
/**
* This statement declares a class1 class, class1 is equivalent to a construction method, also called a constructor
* It can also be said to declare a class1 method
*/
function class1(){
this.name="xjl"; //give Add attributes to the object
this.say= function(){alert("Hello everyone!");}; //Add methods to the object
};
/**
* Use the new keyword to create an instance. The new operator is not only valid for internal classes, but also for user-defined classes.
* Each object can be regarded as multiple attributes ( A collection of methods), that is, object names. Attribute (method) names or object names ["attribute (method) names"]
* Square brackets '[]' are suitable for occasions when you are not sure which attribute (method) to refer to
*/
var a = new class1();
//alert(typeof(a)); //typeof(a) returns the type of a
//alert(a.name); //Each object can be viewed Operation is a collection of multiple attributes (methods),
//alert(a['name']); //Use square brackets ([]) to reference the attributes and methods of the object
//Drop-down box object name [Drop-down box object.value] You can get the value selected by the user. You can also use eval("Drop-down box object name." Drop-down box object. value);
//a.say(); //Call the object's method
//var arr=new Array();
//arr['push']('abc'); //Add an element to the array, where push is a built-in attribute
// arr['push']('1234'); //Add an element to the array
//alert(arr);
/**
* Dynamically add, modify, and delete object properties and methods
*
*/
var obj = new Object() ;
//Add attributes...the attribute names can be arbitrary
obj.name="Xu Jianlong";
obj.sex = 'Male';
obj['my name'] = "xujianlong"; //Use square brackets "[]" to use non-identifier strings as attribute names
//Add methods... The method name can also be chosen arbitrarily, and parameters can also be passed
obj.alert = function(a){
alert(a "Hello!");
}
//Modify the attribute, which is to change the value of the attribute to something else
obj.name = "Zhang San ";
obj['name'] = 'anme';
//Deleting an attribute means changing the attribute value to undefined or null
obj.name = 'undefined';
/* *
* Create untyped objects using brace ({}) syntax
*/
//Enclose attributes and methods in curly brackets, separate attributes with commas, and separate attributes with values ​​by colons
var ob = {
name: "123 ",
say:function(){alert("123")} //The last attribute or method does not need a comma
}
//You can also use the following method to define the attributes and methods of the object
var ob1 = {"name":'123','say':function(){alert("abcd");}};
/**
*prototype prototype object
* The class corresponding to all functions is (Function)
* prototype actually represents a collection of members of a class.
* *When obtaining an object of a class through new, the members of the prototype object will become members of the instantiated object.
*/
function class2(){ //Create an object
}
var ob2 = new class2();
class2.prototype.me = function(){alert("123 ");} //In front of prototype is the class name you created
class2.prototype.name = "123"; //
/**
* The relationship between function objects and other internal objects
*/
//typeof (new Function()),typeof(Function),typeof(Array),typeof(Object) returns the string "function". These parameters are called constructors
//typeof(new Date()), typeof(new Array()), typeof(new Object()) returns the string "object"
/**
* Implicit parameters passed to the function: arguments, which have the characteristics of an array, but it is not an array and can be accessed by subscripts
*/
//arguments contains a parameter callee, which represents a reference to the function object itself , as follows:
var sum=function(n){
if(1==n)
return 1;
else
return n arguments.callee(n-1);
}
//This statement means declaring a namespace of namespace1, as follows:
var namespace1 = new Object();
namespace1.class1 = function(){alert("123");};
var obj1=new namespace1.class1(); //Executed when the page is loaded
/**
* Use prototype objects to define class members
*/
//Use the prototype attribute of the function to define the class after the statement that creates the instance New members will only be effective for objects created later
//The constructor() method in prototype is equivalent to the constructor method
function class1(){
//alert('adf');
}
//class1.prototype.constructor(); //Executed when the page is loaded
//Simplification defined with prototype
class1.prototype={
//Put in some attributes Or method
//Multiple attributes or methods are separated by commas (,)
}
//The following code is static methods and attributes
class1.name="abc";
class1.say = function(){/*codes*/}
//Using the reflection mechanism, the style specified in element can be changed, while other styles will not change, and the desired result is obtained, for example:
function setStyle(_style){
//Get the interface object to change the style
var element=getElement();
for(var p in _style){
element.style[p]=_style [p];
}
}
//Inheritance can be achieved by copying the prototype of one class to another class, but it has flaws. For example:
// function class4(){}
//
// function class2(){
//
//
// class2.prototype=class4.prototype ; //Inheritance of implementation
// class2.prototype.f = function(){alert("a");}
//
//When the prototype of class2 is changed, class4's The prototype also changes accordingly
// instanceof operator to determine whether an object is an instance of a certain class, for example:
var a = new class2();
a instanceof class2; // returns a bool , it is also true if the inherited class in class2 of a
//A better inheritance
for(var p in class1.prototype){
class2.prototype[p]=class1.prototype [p];
}
class2.prototype.ma=function(){
alert(123);
}
//When the prototype of class2 is changed, the prototype of class4 Will not change
/**
* Class inheritance implementation mechanism in prototype-1.3.1 framework
*/
//---------------------------------- -------------------------------------------------- ----------
//This statement adds an extend method to each object. The code is as follows;
Object.extend = function(destination, source) {
for (property in source) {
destination[property] = source[property]; //Assign all properties or methods in source to destination
}
return destination;
}
// Add the method extend for each object through the Object class
Object.prototype.extend = function(object) {
return Object.extend.apply(this, [this, object]);
}
Object.extend.apply(this,[this,object]);
//class1 inherits from class2. The advantage is that using new class2() is equivalent to assigning a copy of the prototype of class2 to class1
//in class1 Changes to the prototype in will not affect the prototype in class2
class1.prototype=(new class2()).extend({/*Attributes or methods to be added to class1*/});
/**
* A method that only declares but does not implement it. A class with virtual functions is called an abstract class. Abstract classes cannot be instantiated
*/
//The virtual method is used directly without declaration. These methods will be implemented in derived classes, for example:
function c1(){}
c2.prototype={
fun:function(){ this.fn();}//The fn method therein Undefined
}
function c2(){}
c1.prototype=(new c2()).extend({
fn:function(){var x = 1;}
});
//this.initialize.apply(this, arguments); This statement is to pass the parameters when creating the object to the initialize method
/***
* In javascript, you can also use the try-catch-finally statement to capture exceptions or error messages
* Among them, the e in the parentheses of catch (e) is required. e is an object named error. Object
* e=new Error(message) to create this object, the description of the exception is used as an attribute message of the error object,
*/
//This code Demonstrates the throwing of exceptions
function s(a,b){
try{
if(b==0)
throw new Error("The divisor cannot be zero! ..... ...");
else
alert(a/b)
}catch(e){
document.write(e.message);///Get the actual value in Error through message Reference
}
}
onlaod=s(1,0);
声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Python vs. JavaScript:開発者の比較分析Python vs. JavaScript:開発者の比較分析May 09, 2025 am 12:22 AM

PythonとJavaScriptの主な違いは、タイプシステムとアプリケーションシナリオです。 1。Pythonは、科学的コンピューティングとデータ分析に適した動的タイプを使用します。 2。JavaScriptは弱いタイプを採用し、フロントエンドとフルスタックの開発で広く使用されています。この2つは、非同期プログラミングとパフォーマンスの最適化に独自の利点があり、選択する際にプロジェクトの要件に従って決定する必要があります。

Python vs. JavaScript:ジョブに適したツールを選択するPython vs. JavaScript:ジョブに適したツールを選択するMay 08, 2025 am 12:10 AM

PythonまたはJavaScriptを選択するかどうかは、プロジェクトの種類によって異なります。1)データサイエンスおよび自動化タスクのPythonを選択します。 2)フロントエンドとフルスタック開発のためにJavaScriptを選択します。 Pythonは、データ処理と自動化における強力なライブラリに好まれていますが、JavaScriptはWebインタラクションとフルスタック開発の利点に不可欠です。

PythonとJavaScript:それぞれの強みを理解するPythonとJavaScript:それぞれの強みを理解するMay 06, 2025 am 12:15 AM

PythonとJavaScriptにはそれぞれ独自の利点があり、選択はプロジェクトのニーズと個人的な好みに依存します。 1. Pythonは、データサイエンスやバックエンド開発に適した簡潔な構文を備えた学習が簡単ですが、実行速度が遅くなっています。 2。JavaScriptはフロントエンド開発のいたるところにあり、強力な非同期プログラミング機能を備えています。 node.jsはフルスタックの開発に適していますが、構文は複雑でエラーが発生しやすい場合があります。

JavaScriptのコア:CまたはCの上に構築されていますか?JavaScriptのコア:CまたはCの上に構築されていますか?May 05, 2025 am 12:07 AM

javascriptisnotbuiltoncorc;それは、解釈されていることを解釈しました。

JavaScriptアプリケーション:フロントエンドからバックエンドまでJavaScriptアプリケーション:フロントエンドからバックエンドまでMay 04, 2025 am 12:12 AM

JavaScriptは、フロントエンドおよびバックエンド開発に使用できます。フロントエンドは、DOM操作を介してユーザーエクスペリエンスを強化し、バックエンドはnode.jsを介してサーバータスクを処理することを処理します。 1.フロントエンドの例:Webページテキストのコンテンツを変更します。 2。バックエンドの例:node.jsサーバーを作成します。

Python vs. Javascript:どの言語を学ぶべきですか?Python vs. Javascript:どの言語を学ぶべきですか?May 03, 2025 am 12:10 AM

PythonまたはJavaScriptの選択は、キャリア開発、学習曲線、エコシステムに基づいている必要があります。1)キャリア開発:Pythonはデータサイエンスとバックエンド開発に適していますが、JavaScriptはフロントエンドおよびフルスタック開発に適しています。 2)学習曲線:Python構文は簡潔で初心者に適しています。 JavaScriptの構文は柔軟です。 3)エコシステム:Pythonには豊富な科学コンピューティングライブラリがあり、JavaScriptには強力なフロントエンドフレームワークがあります。

JavaScriptフレームワーク:最新のWeb開発のパワーJavaScriptフレームワーク:最新のWeb開発のパワーMay 02, 2025 am 12:04 AM

JavaScriptフレームワークのパワーは、開発を簡素化し、ユーザーエクスペリエンスとアプリケーションのパフォーマンスを向上させることにあります。フレームワークを選択するときは、次のことを検討してください。1。プロジェクトのサイズと複雑さ、2。チームエクスペリエンス、3。エコシステムとコミュニティサポート。

JavaScript、C、およびブラウザの関係JavaScript、C、およびブラウザの関係May 01, 2025 am 12:06 AM

はじめに私はあなたがそれを奇妙に思うかもしれないことを知っています、JavaScript、C、およびブラウザは正確に何をしなければなりませんか?彼らは無関係であるように見えますが、実際、彼らは現代のウェブ開発において非常に重要な役割を果たしています。今日は、これら3つの間の密接なつながりについて説明します。この記事を通して、JavaScriptがブラウザでどのように実行されるか、ブラウザエンジンでのCの役割、およびそれらが協力してWebページのレンダリングと相互作用を駆動する方法を学びます。私たちは皆、JavaScriptとブラウザの関係を知っています。 JavaScriptは、フロントエンド開発のコア言語です。ブラウザで直接実行され、Webページが鮮明で興味深いものになります。なぜJavascrを疑問に思ったことがありますか

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

Dreamweaver Mac版

Dreamweaver Mac版

ビジュアル Web 開発ツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!