javascript 자바스크립트 학습은
크게 3가지 부분으로 나눌 수 있습니다: 코어(ECMAscript), 텍스트 객체(DOM), 브라우저 객체(BOM)
코어(ECMAscript): 키워드, 명령문, 연산자, 객체
텍스트 객체(DOM): DOM이 계획을 세웁니다. 전체 페이지를 노드 수준으로 구성된 문서로 만듭니다.
구문 분석은 W3C html dom 표준을 따릅니다.
BOM 브라우저 개체, 팝업 새 브라우저, 브라우저 설정 크기
에 특별한 주의를 기울입니다
.
핵심(ECMAscript) 전역 내장 개체.
메소드:parseInt(), isNan(), encodeURI()...등은 모두 이 객체의 메소드입니다.
eval("alert('hi')")와 같은 동적 언어의 상징인 eval()에 특별한 주의를 기울이십시오. 그러나 이 방법은 매우 사악합니다(보안 측면).
텍스트 객체(DOM) 설명:
<서점> <book Category="cooking"> <title lang="en">일상 이탈리아어</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price > </book> </서점>
1. ECMAscript 기본
$ 가변 약한 유형, 헝가리어 유형 지정: var iOuouValue=100;
$는 세미콜론을 사용하거나 사용하지 않고 줄을 끝낼 수 있지만 onsubmit="javascript:function();return false;
"
"typeof" var test=1; 경고(typeof testX); // "정의되지 않은"
"NaN" 출력 -> isNan("blue") // "true" 출력 ->isNan("123") ; //"false" 출력
$ object var o = new Object();
으로
작성하는 함수가 객체라는 특별한 설명입니다.
이 var a = {name:"Liu Kaiyi"}는 var a = function(){this.name="Liu Kaiyi"};
name:"test" ,pass:"123456",addr:"bj"} //이것은 무엇입니까? ! JSON
var str = '{name:"test",pass:"123456",addr:"bj"}'
도메인 개념을
사용하는 objectBea.name 객체가 있습니다
.<SCRIPT 유형 = 텍스트/자바스크립트>
var sMessage = '안녕하세요';
함수 setSomething() {
sColor = '빨간색';
sMessage = '안녕하세요!';
}
setSomething();
경고(sMessage); //안녕하세요!
경고(sColor); //빨간색
</SCRIPT> <SCRIPT 유형=텍스트/자바스크립트>
var sMessage = '안녕하세요';
함수 setSomething() {
var sColor = '빨간색';
sMessage = '안녕하세요!';
}
setSomething();
경고(sMessage); //안녕하세요!
경고(sColor); // 없음
</SCRIPT>
<SCRIPT 유형=텍스트/자바스크립트>
var sMessage = '안녕하세요';
함수 setSomething() {
var sColor = '빨간색';
var sMessage = '안녕하세요!';
}
setSomething();
경고(sMessage); //안녕하세요
경고(sColor); // 없음
</SCRIPT>
객체지향의 기반 마련: 객체 프로토타입 형태의 객체 적용. 참조
// 가장 간단한 상속
Object.prototype.inObj = 1;
함수 A()
{
this.inA = 2;
}
A.prototype.inAProto = 3;
B.prototype = new A; // A를 B의 프로토타입 체인에 연결합니다.
B.prototype.constructor = B;
함수 B()
{
this.inB = 4;
}
B.prototype.inBProto = 5;
x = 새로운 B;
document.write(x.inObj + ', ' + x.inA + ', ' + x.inAProto + ', ' + x.inB + ', ' + x.inBProto);
//1, 2, 3, 4 , 5
//신뢰도 높이기 http://www.json.org/json.js
Object.prototype.toJSONString = function (filter) { return JSON.stringify(this, filter) }; 그런 다음 bean.toJSONString( )을 사용할 수 있습니다. 그렇지 않나요?
$ 인수 ;
함수 getFun(){alert(arguments.length);} ;
getFun("xx") //출력 1
getFun("xx",23) //출력 2
for(var i=0i<iCount;i++) 또는 for(attr in object)에
대한 특수 지침에 따라 $ 문;
지루하다면 for( sProp in window ){alert(sProp+"Hey, please!");} //javascript의 반영을 살펴보세요.
객체 지향:
var bean = new Bean();
1.팩토리 메소드
함수 getAttr(){
경고(this.attr)
}
function Bean(tattr){
var bean = new Object;
bean.attr =
bean.getAttr = getAttr
;
}
기본적으로 객체지향의 모방 버전입니다.
2.
function Bean(tattr){
this.attr = tattr;
bean.getAttr = function(){
Alert(this.attr);
}
}
위의 2에서 Bean 객체가 생성되면 메소드는 "함수를 반복적으로 생성합니다. "!
3. 프로토타입 모드
함수 Bean(){}
Bean.prototype.attr = "";
Bean.prototype.getAttr=function(){alert(this.attr);}
"중복 생성 함수" 문제를 해결했지만 새로운 문제 Bean.prototype.getArray = new Array();
새로운 객체인 bean1과 bean2는 새로운 배열 공간을 공유하게 됩니다(우리는 이를 보고 싶지 않습니다)
4. 혼합모델 :) ㅎㅎ
function Bean(){
this.attr= "";
this.getArray=새 배열;
}
Bean.prototype.getAttr=function(){alert(this.attr);}
5. 동적 프로토타입(이제부터는 진정한 객체 지향이라는 점에 유의하세요!!!)
function Bean(){
this.attr= "";
this.getArray=new Array;
//로드시 클래스로드
if(typeof Bean._initialized == "정의되지 않음" ){
Bean.prototype.getAttr=function(){alert(this.attr);};
Bean._initialized= true
}
}
/**************************************************** ***** ***************/
객체 상속
1. 객체 사칭! ! (다중 상속을 지원할 수 있으며, copycat은 매우 강력합니다)
function classA(sstr){
this.color = sstr;
this.sayColor = function(){
경고(this.color);
};
}
함수 클래스C(){}
function classB(){
this.newMethod =ClassA;
this.newMethod()
삭제;
this.newMethod =ClassC ;
this.newMethod()
삭제;
this.arrt = "구글";
}
2.call() apply()도 모방입니다.
function classA(sstr){
this.color = sstr;
this.sayColor = function(str){
Alert(str+this.color);
};
}
function classB(){
// this.newMethod =ClassA ;
//
this.newMethod()
삭제 ;
classA.call(this,"red");
"빨간색"))
this.arrt = "바이두";
}
3. 정통 상속 프로토타입 체인(다중 상속은 지원하지 않음)
함수 클래스A(){this.oo="테스트";}
classA.prototype.color = "빨간색";
함수 클래스B(){}
classB.prototype = 새 클래스A;
classB.prototype.sayName = 함수(){
경고(this.color);
}
varbb = 새 클래스B;
bb.sayName(); // 빨간색 출력
경고(bb.oo); //테스트 출력
경고(bb 인스턴스of 클래스A); //참 출력
경고(bb 인스턴스of 클래스B); //참을 출력합니다.
4. 더 많은 상속을 원한다면! ! 또한 instanceof도 지원합니다.
혼합 방법:
함수 클래스A(){}
함수 클래스B(){}
함수 클래스C(){
classA.call(this);
classC.call(this);
}
classC.prototype = new classA;//instanceof는 A에만 사용할 수 있습니다.